Forum Moderators: open

Message Too Old, No Replies

call external .js file from another external .js file

external.js file

         

qazwer

6:21 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



Is it possible to call an external javascript file from another external javascript file?

e.g.

html file code - <script src="file1.js" type="text/javascript">
call whatever() ...

ext javascript file1.js code - function whatever()..
set a cookie ...
alert("whatever")

ext javascript file2.js code - function whatever()..
check if cookie ...
window.open()...

Regards Jerry

monsterhead

8:18 am on Feb 23, 2005 (gmt 0)

10+ Year Member



yes it is possible

SpaceFrog

8:31 am on Feb 23, 2005 (gmt 0)

10+ Year Member



Possible?
Not directly, I don't think so...
You would have to pass thru a popup, this would involve opening a window in the file1.js and dynamically writing the src link for the file2.js

unless there is a method I do not know ...

SpaceFrog

9:21 am on Feb 23, 2005 (gmt 0)

10+ Year Member



well at second thoughts...

var NewLink = document.createElement('script')
NewLink.src="file2.js"

you should then be able to access functions in file2.js

but probably only with IE as FFX and Moz do not like these src updates for scripts ...

SpaceFrog

10:24 am on Feb 23, 2005 (gmt 0)

10+ Year Member



Her is the results of my exploration:
An html page:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>JS EXTERNE</title>
<script src='file1.js' type='text/javascript' /></script>

</head>

<body>
<input type="button" onclick="fonction1()" value="go" />

</body>
</html>

a first external js file:
file1.js

function fonction1(){
var NewScript=document.createElement('script')
NewScript.src="file2.js"
document.body.appendChild(NewScript);
fonction2();
}

a second js file


function fonction2(){
alert('told you I would get there!')
}

Seem to work also with firfox against all odds!

qazwer

6:39 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Can't make it work on this side Spacefrog. IE debugger gives error: Type mismatch (at the function call of script1)

Tried for several days now before dropping it here at Webmasterworld, but it looks like it always gets stuck at the function call in script1 to that function in script2. Same problem with your code SpaceFrog.

monsterhead you say it is possible, I would appreciate it if you would be so kind to drop an example for code in script1 calling the function in script2.

Here is the actual problem I am trying to solve:
I have several html files in different directories that need to set a temp cookie, but not set from their own directory but set from the root directory. So that is why I make these pages call ext. script1 and now I want ext. script2 in the root to set the cookie. Already had it working with the popwin myself just as Spacefrog suggested. But it's more a band-aid solution cause it involves opening an extra popwin that needs to be closed immediately when the cookie was set. Would be nice if this is possible with two communicating external scripts. Then there is no need to open and close a popwin.

thanks

monsterhead

10:48 pm on Feb 23, 2005 (gmt 0)

10+ Year Member




I will give u a hint: Use javascript to include another javascript. Don't bother with popwin. Instead, do it all in the same window.

SpaceFrog

7:54 am on Feb 24, 2005 (gmt 0)

10+ Year Member



It work perfectly on my side, try this:
in the html page:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>JS EXTERNE</title>
<script src='file1.js' type='text/javascript' /></script>

</head>

<body>
<input type="button" onclick="fonction1()" value="go" />

</body>
</html>

a first js file:
file1.js


function fonction1(){
var NewScript=document.createElement('script')
NewScript.src="file2.js"
document.body.appendChild(NewScript);
fonction2();
}

a second js file

function fonction2(){
alert('her I am!')
}

qazwer

8:13 am on Feb 24, 2005 (gmt 0)

10+ Year Member



Ooops I'm very sorry Spacefrog I made a typo, always type code from scratch to make myself remember better instead of cutting and pasting. Besides that, I think need glasses :-)
I now got it working with all the other things I needed. Thank you for helping me out.

And monsterhead thanks for the hint :-)

qazwer

9:28 am on Feb 24, 2005 (gmt 0)

10+ Year Member



Any of you know if there is some type of referrer thing going on between these 2 external java scripts. Because I thought that in the case of setting a cookie with root dir permission, through the second external script from within the root dir, this would be possible. But it seems that file2 keeps track from where file1 is called from thus again ending up with a cookie set with dir permission of the html file from which it is called from. And that doesn't bring me any further cause you can keep it at one external file to get the same result. Again ending up with the root cookie being set by an html file instead of a second ext javascript file (ugly, but it works). Or is this simply impossible with a cookie set by referring external scripts because of security reasons.

SpaceFrog

1:37 pm on Feb 24, 2005 (gmt 0)

10+ Year Member



normal a js file can only be called by one page, if you leave the page, you lose connection to js file

this is why I develloped this :


function AddLibrary(file){
var NewScript=document.createElement('script')
NewScript.src=file+".js"
document.body.appendChild(NewScript);
}

function fonction1(){
AddLibrary('file2')
fonction2();
}

this allows you to call a js library from your html page

qazwer

8:25 am on Feb 25, 2005 (gmt 0)

10+ Year Member



I don't see how your latest suggestion would solve this new cookie permission problem. Let me evaluate:

SOLVED Problem A with your previous code:

Have several html files in different directories that need to call ext. script1 which in turn calls ext. script2

UNSOLVED Problem B:

Thought when ext. script2 (located in the root dir and with a function to set a cookie) was called by ext. script1 (which in turn was called by one of those several html files) the cookie set by ext. script2 would have root permission. But it does not, it has permission starting from the dir of the html file that called the ext. script1

So now I need to know is there a way to trick the system in thinking the set cookie script was called from the root directory, although in actual fact it is not because I want it to be any html file from any dir. Until now the only thing that works is the solution I started out with namely replacing ext. script2 by an actual html file that opens and closes from within the root to set the root permission cookie. I find it hard to believe that there isn't a way around this.

qazwer

6:27 pm on Feb 27, 2005 (gmt 0)

10+ Year Member



All resolved