jsÔõÑù´ÓurlÖлñÈ¡²ÎÊý
javascript ÌṩÁËÁ½ÖÖ»ñÈ¡ url ²ÎÊýµÄÒªÁ죺ʹÓà window.location.search »ñÈ¡ÅÌÎÊ×Ö·û´®¡£Ê¹Óà url ¹¤¾ßµÄ searchparams ÊôÐÔ¡£×¢ÖØ£º»ñÈ¡µ½µÄ²ÎÊýÖµ¶¼ÊÇ×Ö·û´®ÀàÐÍ£¬ÐèҪƾ֤ÐèÒª¾ÙÐÐÀàÐÍת»»¡£
ÔõÑùÔÚ JavaScript ÖÐ´Ó URL ÖлñÈ¡²ÎÊý
JavaScript ÌṩÁ˶àÖÖÒªÁì´Ó URL ÖлñÈ¡²ÎÊý£¬ÒÔÏÂÊÇÁ½ÖÖ³£¼ûÒªÁ죺
1. ʹÓà window.location.search
window.location.search ÊôÐÔ·µ»Ø URL ÅÌÎÊ×Ö·û´®µÄ²¿·Ö£¬ÆäÖаüÀ¨ URL ²ÎÊý¡£ÅÌÎÊ×Ö·û´®ÒÔÎʺŠ(?) ¿ªÍ·£¬ºóÃæËæ×ŲÎÊýÃûºÍÖµ¶Ô¡£
Óï·¨£º
const searchParams = window.location.search;
µÇ¼ºó¸´ÖÆ
Ó÷¨£º
// »ñÈ¡ÅÌÎÊ×Ö·û´® const searchParams = window.location.search; // ÌáÈ¡²ÎÊý const params = new URLSearchParams(searchParams); // »ñÈ¡Ìض¨²ÎÊýµÄÖµ const value = params.get("param_name");
µÇ¼ºó¸´ÖÆ
2. ʹÓà URL ¹¤¾ß
URL ¹¤ÏêϸÏÖÒ»¸ö URL£¬ËüÌṩÁË searchParams ÊôÐÔ£¬ËüÊÇÒ»¸ö°üÀ¨ URL ²ÎÊýµÄ¹¤¾ß¡£
Óï·¨£º
const url = new URL("https://example.com?param1=value1&param2=value2");
µÇ¼ºó¸´ÖÆ
Ó÷¨£º
// »ñÈ¡ URL ¹¤¾ß const url = new URL("https://example.com?param1=value1&param2=value2"); // »ñÈ¡²ÎÊý¹¤¾ß const params = url.searchParams; // »ñÈ¡Ìض¨²ÎÊýµÄÖµ const value = params.get("param_name");
µÇ¼ºó¸´ÖÆ
×¢ÖØ£º
ÒÔÉÏÒªÁ춼»á·µ»Ø²ÎÊýµÄÖµ×÷Ϊ×Ö·û´®¡£ÈôÊÇÄúÐèÒª½«Öµ×ª»»ÎªÁíÒ»ÖÖÀàÐÍ£¨ÀýÈçÊý×Ö£©£¬ÇëʹÓÃÊʵ±µÄÀàÐÍת»»º¯Êý¡£
ijЩÌØÊâ×Ö·û£¨ÀýÈç¿Õ¸ñºÍÌØÊâ·ûºÅ£©ÔÚ URL ÖÐÐèÒª¾ÙÐбàÂ룬ÒÔ×èÖ¹±¬·¢¹ýʧ¡£
ÒÔÉϾÍÊÇjsÔõÑù´ÓurlÖлñÈ¡²ÎÊýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡