/    Sign up×
Community /Pin to ProfileBookmark

setting filename as strings.

Apparently this isnt possible:

<SCRIPT>
var filen = “data.js”;
</SCRIPT>

<script SRC=’filen’ type=”text/javascript”></script>

So how do I make it load a file named a string? The thing is – I need it to load 1data.js, 2data.js, etc. – depending on what a variable is set to in the script.

to post a comment
JavaScript

17 Comments(s)

Copy linkTweet thisAlerts:
@balloonbuffoonApr 12.2006 — This was found at http://www.faqts.com/knowledge_base/view.phtml/aid/1492
[code=html]<html>
<head>
<title>
script loading
</title>
<script type="text/javascript">
function loadScript (url) {
if (document.layers)
window.location.href = url;
else if (document.getElementById) {
var script = document.createElement('script');
script.defer = true;
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
}
}
</script>
</head>
<body>
<form name="gui">
<input type="button" value="load script"
onclick="loadScript('whatever.js')"
/>
<input type="button" value="call f"
onclick="f();"
/>
</form>
<p>
Kibology
</p>
</body>
</html>[/code]


--Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — Good solution for external JavaScript libraries... Anybody know a solution for adding executable script -- without using [b]eval()[/b] -- that, say, is retrieved as source code via an Ajax request? All I know is that this is one case where [b]innerHTML[/b] will NOT work for this purpose. ?
Copy linkTweet thisAlerts:
@TarantinoauthorApr 12.2006 — Hmm, thats not working for me.

Is there any other way to load a file in javascript? Its really just a file with a bunch of variables I need to pass into the script on the side. Maybe some kind of include command that isnt called from <SCRIPT> tag? I cant find it on google.
Copy linkTweet thisAlerts:
@hyperliskApr 12.2006 — Why don't you just use
<i>
</i>&lt;script language="JavaScript" src="my.js"&gt;

What is wrong with doing it like that?
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — Hmm, thats not working for me.

Is there any other way to load a file in javascript?[/QUOTE]

The only other way I know of is during page load -- using [b]document.write[/b] to add the entire SCRIPT tag as a whole.
Copy linkTweet thisAlerts:
@TarantinoauthorApr 12.2006 — Why don't you just use
<i>
</i>&lt;script language="JavaScript" src="my.js"&gt;

What is wrong with doing it like that?[/QUOTE]


I plan on having +100 datafiles. Whatever is clicked on the page should decide what file is loaded and what variables from the file is set to.
Copy linkTweet thisAlerts:
@stephen1davisApr 12.2006 — well, a simple answer is this. i don't know if it's what you want, though...

[CODE]<script type="text/javascript">

var filen = "data.js";
document.write("<script SRC='"+filen+"' type='text/javascript'></script>");

</script>[/CODE]
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — There would be a link here, but apparently, my site is "a competing site". I guess they are scared of my 13 registered members.[/QUOTE]
Please excuse me for saying so, but I'd give serious consideration to growing up and change that little gripe, of yours, too. Do you think IBM should give free advertising to whomever their smallest competitor may be? Do you think MS should give free advertising to whomever the smallest competitor for MS Office may be? In addition, if these big name companies were to give free advertising for their smallest competitors, do you not see how that is an endorsement of the competitor? Seems pretty unreasonable, to me, if such a small competitor were to go all sour grapes over getting refused free advertising. Plus, if this small competitor were to start taking out more free advertising griping about getting refused, me thinks the world in general would certainly get an immediate picture of the supposed "quality" of the ownership of such a small business -- and it would NOT be a *good* picture.
Copy linkTweet thisAlerts:
@balloonbuffoonApr 12.2006 — Good solution for external JavaScript libraries... Anybody know a solution for adding executable script -- without using [b]eval()[/b] -- that, say, is retrieved as source code via an Ajax request? All I know is that this is one case where [b]innerHTML[/b] will NOT work for this purpose. ?[/QUOTE]I found this at http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/insertadjacenthtml.asp

I haven't tried it but it looks promising.[CODE]var sHTML="<input type=button onclick=" +
"go2()" + " value='Click Me'><BR>"
var sScript='<SCRIPT DEFER>'
sScript = sScript +
'function go2(){ alert("Hello from inserted script.") }'
sScript = sScript + '</script' + '>';
ScriptDiv.insertAdjacentHTML("afterBegin",sHTML + sScript);[/CODE]


--Steve
Copy linkTweet thisAlerts:
@TarantinoauthorApr 12.2006 — well, a simple answer is this. i don't know if it's what you want, though...

[CODE]<script type="text/javascript">

var filen = "data.js";
document.write("<script SRC='"+filen+"' type='text/javascript'></script>");

</script>[/CODE]
[/QUOTE]


The problem is that most of my code is in the <HEAD>- and the code needs the variables from the file- with this aproach - the file isnt loaded before it gets to <BODY>
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — I found this at http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/insertadjacenthtml.asp

I haven't tried it but it looks promising.[/QUOTE]

That page says "there is no public standard that applies to this method". Is this still correct? Is [B]insertAdjacentHTML()[/B] not part of the standards?
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — That doesn't explain how you got your script inserted into the page. That is only how to retrieve information from the server via Ajax.
Copy linkTweet thisAlerts:
@balloonbuffoonApr 12.2006 — That page says "there is no public standard that applies to this method". Is this still correct? Is [B]insertAdjacentHTML()[/B] not part of the standards?[/QUOTE]I wouldn't know, today is the first time I've heard of it.

--Stephen
Copy linkTweet thisAlerts:
@TarantinoauthorApr 12.2006 — That doesn't explain how you got your script inserted into the page. That is only how to retrieve information from the server via Ajax.[/QUOTE]

I guess I explained poorly what I wanted then. This does what I wanted:-)
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — <script SRC='filen' type="text/javascript"></script>

The thing is - I need it to load 1data.js, 2data.js, etc. - depending on what a variable is set to in the script.[/QUOTE]

Well! ? If I had known you just wanted to load a data file, I would have mentioned the XMLHttpRequest object at once. ?
Copy linkTweet thisAlerts:
@hyperliskApr 12.2006 — Please excuse me for saying so, but I'd give serious consideration to growing up and change that little gripe, of yours, too. Do you think IBM should give free advertising to whomever their smallest competitor may be? Do you think MS should give free advertising to whomever the smallest competitor for MS Office may be? In addition, if these big name companies were to give free advertising for their smallest competitors, do you not see how that is an endorsement of the competitor? Seems pretty unreasonable, to me, if such a small competitor were to go all sour grapes over getting refused free advertising. Plus, if this small competitor were to start taking out more free advertising griping about getting refused, me thinks the world in general would certainly get an immediate picture of the supposed "quality" of the ownership of such a small business -- and it would NOT be a *good* picture.[/QUOTE]

I have no want or need to grow up right now. I am at an age where I can be immature if I fell like it, and hey, I do feel like it. But anyway, in response to this:

Anybody know a solution for adding executable script -- without using eval() -- that, say, is retrieved as source code via an Ajax request? All I know is that this is one case where innerHTML will NOT work for this purpose.
[/QUOTE]

I have wrote some code, but you will need to fix the reg exp, because I couldn't figure it out totally, and I am sure you can. Right now it just will give you the last script that was in the document you loaded, and it will insert it into the current document.
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script language="javascript"&gt;

var fs=false;

if(typeof ActiveXObject != "undefined"){
try {
fs = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
fs = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
fs = false;
}
}
}
if (!fs &amp;&amp; typeof XMLHttpRequest != "undefined") {
try {
fs = new XMLHttpRequest();
} catch (e) {
fs=false;
}
}
if (!fs &amp;&amp; window.createRequest) {
try {
fs = window.createRequest();
} catch (e) {
fs=false;
}
}

function getScript(){

fs.open("GET", "myfile.html", true);
fs.onreadystatechange = function(){
if (fs.readyState==4){
text = fs.responseText;
alert(text)
//[\w|\W]+ [\w|\W]+
r = new RegExp("[\w|\W]+&lt;script.*&gt;([\w|\W]+)&lt;/script&gt;[\w|\W]+","i");
scripts = new Array();
while(text.match(r)){
scripts[scripts.length] = text.replace(r,"$1");
text = text.replace(r,"-");
alert(scripts);
}
newScript = document.createElement("script");
newScript.language = "JavaScript";
theScript = document.createTextNode(text);
newScript.appendChild(theScript);
document.getElementsByTagName("head")[0].appendChild(newScript);
}
}
fs.send(null);

}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="button" onClick="getScript()" value="Click Me!"&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @Tarantino spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...