SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Greece
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    calling a script from another page

    Is there any way I can call a JavaScript function located in page1.html inside page2.html? For example:

    //page1.html
    ...
    function foo()
    {
    ...
    }
    ...


    //page2.html
    ...
    foo();
    ...

    That's obviously wrong, is there any way I can solve this problem?

    Thank you!

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi themis,
    You can put foo() in an external foo.js file and include it in all the HTMLs that needs it.

    Add this to page1.html & page2.html:
    Code:
    <SCRIPT LANGUAGE="JavaScript" SRC="foo.js"></SCRIPT>
    Of course, foo.js can have other js functions in it too.

    Daniel

  3. #3
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Greece
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by danielng5
    Hi themis,
    You can put foo() in an external foo.js file and include it in all the HTMLs that needs it.

    Add this to page1.html & page2.html:
    Code:
    <SCRIPT LANGUAGE="JavaScript" SRC="foo.js"></SCRIPT>
    Of course, foo.js can have other js functions in it too.

    Daniel
    : Ja, you can put a src into a script tag: <script language="javascript" src="page2.js">. I don't think you can get a function from an embedded Javascript in another page, but you can link to it from both pages.
    :

    Hi, I used the above example so I transfered all my functions in script.js. Now I write the following in page1.html
    Code:
    <script language="javascript" src="script.js">
    </--
    my_func();
    //-->
    </script>
    however my function isn't called. Can you understand what I'm doing wrong?

    Thanks!

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're confusing two different things. When JavaScript is running, it doesn't 'get' program data from embedded or 'external' scripts - it gets it from memory. If you reference a function (or any other object) it'll look up that reference in whatever memory space has been allotted to run JS in. Scripts that are currently unloaded might as well be on Mars - JavaScript doesn't read files.

    There are some wrinkles to this, involving the dynamic loading of JS files, but for the most part that's the way the game is played. You didn't explain the relationship between 'page1.html' and 'page2.html', but they'd have to be loaded into memory (i.e., into windows) at the same time for a function declared in one to be called from the other. If not, extracting the code to a separate file, loaded with the SRC="..." attribute, is a reasonable way to go. You never embed (literally code) script statements or data in between <SCRIPT></SCRIPT> tags when a SRC has been set - that's the whole idea (the script in question is not being included in the currently loaded page). You'll need an entirely separate script block to embed the function call in:
    Code:
    <script type="text/javascript" src="script.js"></script>
    <script type="text/javascript">
    my_func();
    </script>
    Scripts are loaded - one at a time - from 'top-to-bottom' (actually 'beginning to end') as you read down through your source code. Make sure the order is correct, so you're not referencing anything that hasn't been loaded yet. No HTML in .js files - just pretend their content is actually right there, in between those <SCRIPT></SCRIPT> tags.
    ::: certified wild guess :::

  5. #5
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    595
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Hope this isn't OT, but I do have a similar problem. At present there is an include for every html page, it contains much 'header' info, but the JS part is

    header-content-pages.inc

    Code:
     <head>
     ... lots of stuff ...
     <script type="text/javascript" src="order-script.js"></script></head>
    I need to add the script at http://javascript.internet.com/cooki...opup-once.html , and rather than add the same code by including the new script in header-content-pages.inc , it seems to make sense to modify

    header-content-pages.inc ... to

    Code:
      <head>
      ... lots of stuff ...
      <script type="text/javascript" src="order-script.js"></script>
      <script type="text/javascript" src="popup-once.js"></script></head>
    and of course create a file called popup-once.js, with the above mentioned JS code in it.

    Firstly, can I do this, that is, have multiple "<script>...</script>" lines ? It would seem to be more efficient this way, rather than drag all the JS code into _every_ html, just a one line to reference the JS file.

    Secondly, will it work ? That is, what happens when a JS function is called, does it go through order-script.js looking for _that_ function, and then look through popup-once.js to eventually find the function ? If so, because the 'orders' functions will be used a lot less than the 'popup once', it would be more efficient to place the order (no pun intended) of the JS files around the other way, wouldn't it ?

    There is another include that I will modify, just to place the following

    Code:
     <BODY onUnLoad="checkCount()">
    so that every page will perfrom the JS function, when the form is closed.

    Any other issues I need to be aware of ?

    Thanks,

    Peter

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not really OT - more of a 'hijack' Why not start your own thread?

    Hmm...
    what happens when a JS function is called, does it go.....looking for _that_ function, and then look.......to eventually find the function ?
    Encore:
    If you reference a function (or any other object) it'll look up that reference in whatever memory space has been allotted to run JS in.
    Again - you're confusing the content of an unloaded file containing JavaScript with JavaScript statements & data actually loaded into memory. The script interpreter doesn't read files, it accesses the assigned JS memory space and manipulates JS objects and primitive values loaded there. If you load script A and then script B, a function from script A is not 'earlier' than one from script B: it's in there, or it ain't in there. btw, these are not 'includes' (which has a specific meaning).

    Second question:

    <script type="text/javascript" src="check_count.js"></script>

    ...in every file, where 'check_count.js' contains:
    Code:
    onunload = function()
    {
         ....contents of checkCount() function....
    }
    ...is the same as:

    <BODY onUnLoad="checkCount()">

    ...in every file. Just be aware that you're assigning the window.onunload event handler, which can only be done once per document (without some fancy gymnastics, anyway).

    For more real-world info on the loading of JS, just hit that 'view-source' option on some of your favorite sites. There are dozens of issues to be aware of including getting visible content up as quickly as possible (to hold visitors) to delaying image preloads as long as possible to...well, you get the picture.
    ::: certified wild guess :::

  7. #7
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    595
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Quote Originally Posted by adios
    you're confusing the content of an unloaded file containing JavaScript with JavaScript statements & data actually loaded into memory.
    So, when do the (JS) files get loaded into memory ? Is it when the first "<script>....some JS filename</script>" is read by the client, or when the client activates a (JS) event ?

    The script interpreter doesn't read files, it accesses the assigned JS memory space and manipulates JS objects and primitive values loaded there.
    Yes, but the interpreter could only use the memory contents/values that are placed there in the first place, the original source being the (JS) files.

    If you load script A and then script B, a function from script A is not 'earlier' than one from script B: it's in there, or it ain't in there.
    Just helps knowing if it is similar to a 'stack' (earlier or later _would_matter), or pre-assigned memory addreses, that's all.

    btw, these are not 'includes' (which has a specific meaning).
    The includes have the "<body>" statements, which have the JS events, to call a JS function.

    Second question:

    <script type="text/javascript" src="check_count.js"></script>

    ...in every file, where 'check_count.js' contains:
    Code:
     onunload = function()
     {
          ....contents of checkCount() function....
     }
    ...is the same as:

    <BODY onUnLoad="checkCount()">

    ...in every file.
    Both "<script>" and "<body>" tags are in every (html) file. I would need the 'onUnLoad' event to be checked in every form/page, and of course I therefore need to tell JS where to "go get it".

    For more real-world info on the loading of JS, just hit that 'view-source' option on some of your favorite sites. There are dozens of issues to be aware of including getting visible content up as quickly as possible (to hold visitors) to delaying image preloads as long as possible to...well, you get the picture.
    Thanks, I may consider calling some JS from Perl (I'm fairly sure you can do that, _force_ an event, from Perl ??) in one of the forms we use. It has to do a lot of processing, and there is no "gas gauge" or "please wait". The logs from Perl indicate some people are exiting before all the processing is done, which for an online order, is, well, not what we want.

    Thanks for your help,

    Pedro (Peter)

  8. #8
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great explanations by adios!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •