I'm sure this is simple but here is what I need to do:

I want to change a variable within the source url. Heres an example of what I'm trying to say:

<script type="text/javascript" src="url.com?lang=en&X=word1&;"></script>

This works fine, but I want the variable X to be elsewhere, like this:

<script type="text/javascript" src="url.com?lang=en&X&;">
X=word;
</script>

How do I go about doing this?

Recommended Answers

All 4 Replies

are you saying that you want to "read" the "X" from the url and make it into a JS variable?

No, what I'm trying to do is make it so the "X" can be changed elsewhere in the code and call it within the URL

in that case you need to dynamically insert the script tag:

<html>
<head>
<script>
var X='hello';

function importScript(url){
 var headID = document.getElementsByTagName("head")[0];         
 var newScript = document.createElement('script');
 newScript.type = 'text/javascript';
 newScript.src = url;
 headID.appendChild(newScript);
}

window.onload=function(){
  importScript('url.com?lang=en&X='+X+'&');
};
</script>
</head>
<body>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.