Reputation: 33655
I have the following script declared in the document of my head:
<script src="http://www.domain.com/js/widgets.js" type="text/javascript"></script>
Which points to this:
widgets.js
(function () {
var styleEl = document.createElement("link");
styleEl.type = "text/css";
styleEl.href = "http://127.0.0.1:8002/static/css/widgets.css";
styleEl.rel = "stylesheet";
//document.getElementsByTagName('head')[0].appendChild(styleEl);
document.head.appendChild(styleEl);
document.write("<div id='share_box'>" +
"<a href='test' target='_blank'>R</a>" +
"</div>");
})();
I would like to pass in variable to this external file, this is what I have tried:
<script src="widgets.js" type="text/javascript">
var url = 'https://www.domain.com/link/'
</script>
How can I pass in the variable URL to my external script? (in a safe way using javascript only - no jquery)
Upvotes: 0
Views: 1466
Reputation: 15860
The links you're providing are both imaginary. There is nothing present there, however the code that you're having is as:
(function () {
var styleEl = document.createElement("link");
styleEl.type = "text/css";
styleEl.href = "http://127.0.0.1:8002/static/css/widgets.css";
styleEl.rel = "stylesheet";
//document.getElementsByTagName('head')[0].appendChild(styleEl);
document.head.appendChild(styleEl);
document.write("<div id='share_box'>" +
"<a href='test' target='_blank'>R</a>" +
"</div>");
})();
Actually, you cannot pass a parameter to it. Because it doesn't allow any.
To recieve a parameter, the function must allow a parameter while declaration. Such as:
function someFuncName (params) {
/* then you can say that the parameters would be sent! */
}
The code you're saying, won't allow any parameters because there isn't any space or permission for a param.
However, to pass on a param, you use the specific term
<script>
functionName(params);
</script>
Simple it is. But in your case, you cannot!
What the above code actually does, is that is adds a link element. Such as
<link type="text/css" href="http://127.0.0.1:8002/static/css/widgets.css"
rel="stylesheet" />
And the next thing it does, is that it would write an element clause of
<div id="share_box">
<a href="test" target="_blank">R</a>
</div>
Maybe the above code, would create a hyperlink and then style it using the .css
file provided at the site that is inside the href.
But the answer is still, that you cannot pass the parameter to an external file until or unless it allows you to.
Upvotes: 1