admin管理员组文章数量:1327487
Example: Suppose the current page url(window.location.href) is .html The html page source code is...
<html><head></head><body>
<script src=".js?user=Ankit&ptid=18"></script>
</body></html>
Now I need to use 'src' variables in script.js And the script file script.js should return
var a="Ankit"
var b="18"
Can we use something like echo $_GET like in php?
Example: Suppose the current page url(window.location.href) is http://example./page.html The html page source code is...
<html><head></head><body>
<script src="http://example./script.js?user=Ankit&ptid=18"></script>
</body></html>
Now I need to use 'src' variables in script.js And the script file script.js should return
var a="Ankit"
var b="18"
Can we use something like echo $_GET like in php?
Share Improve this question edited May 14, 2013 at 13:43 Ankit_Shah55 asked May 14, 2013 at 13:27 Ankit_Shah55Ankit_Shah55 8272 gold badges10 silver badges17 bronze badges 5- 4 possible duplicate of How can I get query string values? – Cyril N. Commented May 14, 2013 at 13:29
- I didn't get it what u said – underscore Commented May 14, 2013 at 13:29
- see developer.mozilla/en-US/docs/DOM/window.location – Lim H. Commented May 14, 2013 at 13:29
- possible duplicate of stackoverflow./questions/247483/… – aynber Commented May 14, 2013 at 13:30
- stackoverflow./questions/2090551/… – Nicholas Decker Commented May 14, 2013 at 13:44
3 Answers
Reset to default 5Found this here. If you're using jQuery, this should be helpful.
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
This is a javascript function that will return the value in the url of a parameter that you pass to it. In this case, you would call it with
var a = getURLParameter("user");
var b = getURLParameter("ptid");
EDIT: I misinterpreted the original version of your question as asking about getting parameters to the .html page being loaded. I just tested this solution, and it does not work within the .js file itself. However, if you declare your variables in the .js file, and place this in the onLoad event, removing var
from in front of a
and b
, it should assign the variables correctly.
Maybe outdated but a nice piece of code and would exactly do what was asked for in OP
// Extract "GET" parameters from a JS include querystring
function getParams(script_name) {
// Find all script tags
var scripts = document.getElementsByTagName("script");
// Look through them trying to find ourselves
for(var i=0; i<scripts.length; i++) {
if(scripts[i].src.indexOf("/" + script_name) > -1) {
// Get an array of key=value strings of params
var pa = scripts[i].src.split("?").pop().split("&");
// Split each key=value into array, the construct js object
var p = {};
for(var j=0; j<pa.length; j++) {
var kv = pa[j].split("=");
p[kv[0]] = kv[1];
}
return p;
}
}
// No scripts match
return {};
}
Source: James Smith - Extract GET Params from a JavaScript Script Tag
I know it's an old post, but as I was looking for something like that I came across it. The very simple solution I finally adopted is the following one:
<html><head></head><body>
<script>
var a = "Ankit";
var b = 18;
</script>
<script src="http://example./script.js?user=Ankit&ptid=18"></script>
</body></html>
If you absolutely want to plicate your life and use Lahmizzar's solution, I would remend to give an id to your tag script, which avoids a greedy function.
HTML :
<script src="http://example./script.js?user=Ankit&ptid=18" id="myScript"></script>
JS :
function getParams(script_id) {
var script = document.getElementById(script_id);
if(script) {
// Get an array of key=value strings of params
var pa = script.src.split("?").pop().split("&");
// Split each key=value into array, the construct js object
var p = {};
for(var j=0; j<pa.length; j++) {
var kv = pa[j].split("=");
p[kv[0]] = kv[1];
}
return p;
}
// No scripts match
return {};
}
getParams("myScript");
本文标签: phpHow to get variable from url for js fileStack Overflow
版权声明:本文标题:php - How to get variable from url for .js file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742204759a2432608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论