简体   繁体   English

从URL获取值并创建Javascript函数

[英]Get a value from URL and create a Javascript function

I am new on this website, and I am also new at Javascript. 我是这个网站的新手,也是Javascript的新手。 What I would do is get a value from a link, eg: http://bricks.couponmicrosite.net/JavaBricksWeb/LandingPage.aspx?O=107905&C=MF&CPT=qwbc74g7HdLtKVVQ1oNe&P=test&tqnm=td3ffdn764156741 我要做的是从链接中获取值,例如: http : //bricks.couponmicrosite.net/JavaBricksWeb/LandingPage.aspx?O=107905&C=MF&CPT=qwbc74g7HdLtKVVQ1oNe&P=test&tqnm=td3ffdn764156741741

I need to take this value: O=107905, if it is only one code I need to run a file (file A), 我需要采用以下值:O = 107905,如果只有一个代码,我需要运行一个文件(文件A),

if it look like this: O=107905~107906, then I need to run a different file (file B). 如果它看起来像这样:O = 107905〜107906,那么我需要运行另一个文件(文件B)。

What I thought is create a function with javascript where if it is (~) is missing, then will be used the file A, if there is one or more than one of (~) then the file B will start to work. 我以为是用javascript创建的函数,如果缺少(〜),将使用文件A,如果其中一个或多个(〜),则文件B将开始工作。

Many thanks in advance! 提前谢谢了!

Well. 好。 We really do encourage you to provide your own code first before providing solutions, but this is a fairly simple problem in javascript. 我们确实鼓励您在提供解决方案之前先提供自己的代码,但这在javascript中是一个非常简单的问题。 Here's my take on it. 这是我的看法。

The first problem you have to solve is actually getting the query string parameters in a meaningful format. 您必须解决的第一个问题是实际上以有意义的格式获取查询字符串参数。 Here I create an object that uses the query string keys as the object keys (IE 9+ only because of the forEach ) 在这里,我创建一个使用查询字符串键作为对象键的对象(IE 9+仅由于forEach

var tmpObject = {}
location.search.substr(1).split('&').forEach(function(item){
    var o = item.split('=');
    tmpObject[o.shift()] = o.shift();
});

Then it's just a matter of making sure the query string contained the target object (in this case "O") and determine if there is more than one. 然后,只需确保查询字符串包含目标对象(在本例中为“ O”),然后确定是否有多个对象即可。

if(tmpObject.hasOwnProperty('O') && tmpObject.O.split('~').length > 1) {
    console.log('return multiple files');
} else {
    console.log('return one file');
}

Try this 尝试这个

var url = "http://bricks.couponmicrosite.net/JavaBricksWeb/LandingPage.aspx?O=107905&C=MF&CPT=qwbc74g7HdLtKVVQ1oNe&P=test&tqnm=td3ffdn764156741";
var search = url.substring(url.indexOf("?")+1);

var map={};
search.forEach(function(val){var items=val.split("="); map[items[0]]=items[1];});

if (map["O"].indexOf("~") != -1)
{
  //O value has ~ in it
}
else
{
  //O has no ~ in it 
}

Possible solution to get the paramter would be there : How to get the value from the GET parameters? 获取参数的可能解决方案将是: 如何从GET参数获取值?

Once you have the parameter value, you can for sure look if you find '~' with String.prototype.indexOf. 一旦有了参数值,就可以确定是否通过String.prototype.indexOf找到了“〜”。

String.prototype.indexOf returns the position of the string in the other string. String.prototype.indexOf返回该字符串在另一个字符串中的位置。 If not found, it will return -1. 如果找不到,它将返回-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM