code :
<script type="text/javascript" src="http://127.0.0.1/Test.js#username=stackoverflow">
</script>
iwant to know ,how to get the username in Test.js
file
Test.js :
var username = ??
///////////// #username=stackoverflow
thanks advance
If you are trying to do all this on the client side, it's much better to use:
<script type="text/javascript">//<![CDATA[
var username = "stackoverflow";
//]]></script>
<script type="text/javascript" src="http://127.0.0.1/Test.js"></script>
That way, you don't need to tackle the issue of reading the src attribute of the script tag somehow.
The query portion of the URL is invalid. It should be:
http://127.0.0.1/Test.js?username=stackoverflow
The # is treated as a named anchor.
The gup function isn't good because the parameter is on the script tag, not the HTML output page.
The location object (location.href, location.search...) refers to the HTML page where the script included.
There are 2 other options:
Use this
Use #idealmachine answer. You can wrap the global variable with simple object in order to avoid conflict with other global JS variables
Related
So I have a script that is loaded dynamically into the page as so:
<div>
<script>
var url = 'http://example.com/scripts/myscript.js?foo=bar';
document.write('<scr'+'ipt type=\"text/javascript\" src=\""+url+"\"></scri'+'pt>')
</script>;
</div>
I need to know, if there is a way, inside the myscript.js i am loading, to access the url to get the query string.
Thanks
When a script is running (unless you add defer or async attributes), it will always be the last element on the page so far. You can therefore take advantage of this fact:
var scripts = document.getElementsByTagName('script'),
currentScript = scripts[scripts.length-1],
myScriptURL = currentScript.getAttribute("src");
You can then process that myScriptURL variable to extract query string paramters, as demonstrated in this question.
I have a javascript that sits on my server. I want to provide my visitors with javascript code that they can place on their servers in the way that Google Analytics does it. For example:
<script type="text/javascript" src="http://www.somedomain.com/script/script.js?id=2001102"></script>
I got everything working up to the point where I need to grab the id. I'm just not sure what to use for that.
I tried both location.href and location.search, but that gives me url + param of the file where the script is embeded, not "script.js?id=XSOMEIDX"
In script.js I have the following:
function requestContent() {
var script = document.createElement("script");
script.src = "http://www.somedomain.com/script/xss_script.php?id="I WANT TO INPUT ID HERE+"&url="+location.href;
document.getElementsByTagName("head")[0].appendChild(script);
}
Any how I can take id=XSOMEIDX and put it in xss_script.php?id= ?
Thanks in advance!
You can use URL rewritting to take id=XSOMEIDX and put it in xss_script.php?id=
A mod rewrite rule doing it would look like this :
RewriteRule ^/scripts/([a-zA-Z0-0]+)/script.js$ /scripts/script.php?id=$1
This way you could simply ask the people to include yoursite.com/scripts/{id}/scripts.js
how about setting up the external script tag with a certain attribute?
<script data-my-script="my_value" type="text/javascript" src="http://www.somedomain.com/script/script.js?id=2001102"></script>
in modern browsers you can then do
var scripts = document.querySelectorAll("script[src][data-my-script]");
$.each(scripts, function(i, script) { console.log(script.src); });
and iterate over the nodeList...
NOTE: querySelectorAll is not working cross-browser
NOTE: querySelectorAll is returning an array-like object
I am writing a third party javascript (my.js) that can be inserted in a HTML page using script tag. I want to achieve the following:
my.js gets loaded (which has a function myFunc(params))
myFunc() gets called with appropriate params (parameters can change)
putting my.js script in head is not an option
What is the best approach that I can use?
The problem is that you can't really pass parameters w/ just 1 script tag pointing to an external file, so you would have to get them from some element in the DOM:
The html:
<html>
<body>
<script src="my.js"></script>
<input id="params" type="hidden" value="'param1', 'param2', 'param3'" />
<div id="result"></div>
</body>
</html>
The javascript:
function myfunc() {
var doc = document,
params = doc.getElementById("params").value.split(","); // make an array of params
doc.getElementById("result").innerHTML = params.toString();
}
window.onload = myfunc;
Honestly though, this is a kludge. As mentioned before by Felix, you should probably just use 2 script tags -- One to get the external js file and one to call the function with the parameters you need.
You can pass parameters in via the query string and parse them out dynamically.
For example, your script tag becomes:
<script src="my.js?foo=bar"></script>
You can then get the value of the URL using:
var scripts = document.getElementsByTagName('script');
var url = scripts[ scripts.length - 1 ].getAttribute('src');
Because of the order JS is loaded by the browser, the last script on the page (while your script is executing during load) should always be your script.
Then you parse the query string. There are a bunch of questions on Stack Overflow dealing with that. Ex:
Parse query string in JavaScript
{literal}
<SCRIPT LANGUAGE="JavaScript"
SRC="../calendar/weeklycalendar.js">
</script>
<script>
// call the function to build the calendar
// function's param specify the first day of week
// 0=Sunday, 1 = Monday, ..., 6=Saturday
alert("before");
buildWeeklyCalendar(1);
alert("afetr");
</script>
{/literal}
this script runs fine on server but when i use it in smarty template it doesn't work.
Can anyone explain.
Thanks
I figured it out. The js file will be called with reference to the PHP file that calls the template . The path will not be according to where the smart y template is but according to where the calling php file is.
Thanks
Here's an interesting JS q... suppose you have:
host1.html on host1.com which references an external javascript (host2.js) on host2.com. In the host2.js, I'd like to get the location.hostname of the host serving the host2.js, but because host2.js is called inside of host1.html, it returns the location.hostname of host1.
Is there a way to get the location object of the external script within the external script being called?
I think similar questions have been asked before, and the answer was always no, this can't be done.
Workarounds:
Work through the parent document's script elements and, using a counter variable, find out which one we're in (ugh)
Output the current URL into the included script on server side, e.g. in PHP: script_current_url = <?php echo "http://".$_SERVER["HTTP_HOST"]."/".$_SERVER["REQUEST_URI"] (there's a variable for the protocol part too, I just forgot the name)
Set a variable before each <script> tag:
<script type="text/javascript">
script_current_url = "http://www.example.com/include.js";
</script>
<script type="text/javascript" src="http://www.example.com/include.js">
</script>
this is kludgy, but could be simplified by building a JS function that includes the file and sets the right variable automatically.
I like the server-side approach the best, but depending on your platform, it has other implications like having to send all .js resources through a resource-expensive interpreter.