Click to See Complete Forum and Search --> : Loading JS in the same id place..PLZ HELP
nospirit
12-27-2005, 06:21 AM
Dear all
I search all the net about this answer but I could not find any script for it
or if that can be or not ..
we have loaded external js with id ="anyname"
I need to click on button to load another external js in the same place I mean in the same id
please click to see the example :
http://www.creativelink-sy.com/example/test.html
please inform me as soon as possible
thanks in advance
konithomimo
12-27-2005, 06:31 AM
Check you other post about this . . . there are already two solutions there . . .
nospirit
12-27-2005, 07:07 AM
where are they ? I couldn't find any ..
please post me them
thanks
vwphillips
12-27-2005, 07:15 AM
there are also solutions on your duplicate posts at codeingforums and sitepoint
duplicate posts are a pain
konithomimo
12-27-2005, 07:19 AM
The first problem with your code is that you have:
</table></tr></td>
that should be
</td></tr></table>
That being said, the responses were to use either of the following codes:
<html>
<head>
<script type="text/javascript">
function changeSRC()
{
document.getElementById('x').innerHTML = '<script type=\"text/javascript\" src = \"second.js\"></' + 'script>';
}
</script>
</head>
<body>
<input type="button" value="Click here to change JS" onclick="changeSRC()">
<table border="1">
<tr>
<td id="x"><script language=JavaScript src = first.js></script></td>
</tr>
</table>
</body>
</html>
or
<html>
<head>
<script type="text/javascript">
function newJS(){
document.getElementById("replaceMe").src="second.js";
}
</script>
</head>
<body>
<input type="button" value="Click here to change JS" onclick="newJS()">
<table border="1">
<tr>
<td><script type="text/javascript" src="first.js" id="replaceMe"></script></td>
</tr>
</table>
</body>
</html>
methodpg
12-27-2005, 07:36 AM
Just posting purely out of curiosity...
But... in the circumstances outlined in this thread.. would replacing the "src" attribute be enough for that JS to get parsed by browsers?
Or would it just change it within the DOM without parsing it?
I'm only asking because... in the past (before I learnt about appendChild) I tried to re-write the innerHTML of an existing DIV to put a "script" tag in with a function... and some browsers just acted like the new function didn't exist.
Just wondering if this suffers the same problem or not!?!?
konithomimo
12-27-2005, 08:04 AM
NOt sure, but most likely something like that will occur. To be honest, I would just load both JS files into divs and then just change which div is displaying:
<html>
<head>
<script type="text/javascript">
function changeSRC()
{if(document.getElementById('first').style.display=='block')
{
document.getElementById('first').style.display='none';
document.getElementById('second').style.display='block';
}
else
{
document.getElementById('first').style.display='block';
document.getElementById('second').style.display='none';
}
}
</script>
</head>
<body>
<input type="button" value="Click here to change JS" onclick="changeSRC()">
<div id="first" style="display:block">
<script type="text/javascript" src="first.js"></script>
</div>
<div id="second" style="display:none">
<script type="text/javascript" src="second.js"></script>
</div>
</body>
</html>
nospirit
12-27-2005, 08:13 AM
Thanks all ...
about duplicate posts are a pain,,well sorry but I didn't find any answer till now so I have to find out the solutions,so please don't fill pain of that, we are here to share our informations thanks all
NOW: really nothing works...
the fisrt code which konithomimo made.
its load the first.js but when we click on the button, first.js disappeared and no any second.js load ..
any answer ???
I'm afraid that there is no any soultion for it..!!
please help ....
Thanks for all.
konithomimo
12-27-2005, 08:23 AM
My last code worked flawlessly for me. As long as the page,first.js, and second.js are all in the same folder then loading both into divs works perfectly. That and it will perform better if the JS file has a lot of code or images. Since then they are already preloaded.
nospirit
12-27-2005, 09:06 AM
Hi,
Thanks..
yes it works.. but : in this way the second.js is loaded at the same time with first.js but the second was hidden.. so if the second.js is about 500Kb then the page will load the first and the second at the same time... So what I need ...first to load the first.js only then when I click on button...to load second.js which may be more than 800KB...
Thanks for help
methodpg
12-27-2005, 09:58 AM
<HEAD>
<script>
function loadContent(file){
var head = document.getElementsByTagName('head').item(0)
var scriptTag = document.getElementById('loadScript');
if(scriptTag) head.removeChild(scriptTag);
script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.id = 'loadScript';
head.appendChild(script)
}
</script>
</HEAD>
<BODY onLoad="loadContent('first.js')">
<a href="javascript:loadContent('second.js')">Load Script</a>
</BODY>
:)
nospirit
12-28-2005, 05:15 AM
Opps Thanks..but still dosen't work !!!
WelL I'm afraid that there is no any solution for this code.!!!!! strange ...
I hope to find any solution :
http://www.creativelink-sy.com/example/test.html
Thanks for all
nospirit
01-04-2006, 06:15 AM
Please I need a solution ..... any expert out there ?
http://www.creativelink-sy.com/example/test.html
Thanks for all
FromU2ME
01-04-2006, 06:46 AM
I think the question you should ask yourself is why you would like to load a second external js-file at a separate time? :confused:
FromU2ME
01-04-2006, 06:50 AM
However, if you MUST, it's strange no one else thought of loading the second JS-file in an inline frame (iframe) and pass the variables between the main site and the main?
This will only work if it's your script (so that you can alter it and access it -- doesn't work via cross-domains).
--EDIT
Now that I look at your site again, it doesn't seem like you need to use an external JS-file at all - simply change it to an iframe (since you're trying to do the same with the JS-files as can be done with an iframe)...
webstuff
01-04-2006, 09:27 AM
I think the question you should ask yourself is why you would like to load a second external js-file at a separate time? :confused:
deliver a payload,
security,
out of need,
just because...
Take your pick...
nospirit
01-05-2006, 01:49 AM
Well ,Thanks alot for all..
So , I know about the iframe and I know about other solutions,, But What I need is what is in the test page :http://www.creativelink-sy.com/example/test.html
My question is : Can that be or not ??? Maybe what I need doesn't exist...
So I asked a lot of programmers,and no any Solution till now.,...the question why it hasn't solution ??? maybe its a weakness in javascript !!!???
any comments please ...
Thanks