定义位置对象和URL参数的正确URL语法是什么

What is the proper URL syntax to define a location object and a URL parameter?

本文关键字:URL 语法 是什么 参数 位置 对象 定义      更新时间:2023-09-26

我见过很多这样或那样的例子,但没有一个是用这种特定的设置。我的URL中当前有一个位置对象,它通过jquery打开一个表单:http://blah.com/contact.cfm?show_sales

$(document).ready(function(){
    if (window.location.search == "?show_sales") {
        $('#sales_form').show();
    };

我还希望URL中包含一个参数:http://blah.com/contact.cfm?item=4445555

我尝试过两者的组合,但我不知道将两者都包括在内的正确语法。我所尝试的一切都无法激活jquery。

那么,这个URL的正确语法是什么呢?

http://blah.com/contact.cfm?show_sales&项目=4445555

(这是一个不起作用的例子)

URL的格式是正确的。您应该对代码进行一些更改:

$(document).ready(function(){
    if (window.location.search.indexOf("?show_sales")!=-1) {
        $('#sales_form').show();
    };

window.location.search将返回show_sales&item=4445555

您可以使用split:

var url = "http://blah.com/contact.cfm?show_sales&item=4445555";
var query = url.split("?")[1];
var params = query.split("&");
console.log(params); 

// params[0] = "show_sales"
// params[1] = "item=444555" and you can split again