www.webdeveloper.com
Results 1 to 9 of 9

Thread: Trouble Calling Javascript File in HTML

  1. #1
    Join Date
    Feb 2010
    Posts
    6

    Question Trouble Calling Javascript File in HTML

    Basically as the title says, I'm having troubles calling a javascript file into my HTML page.

    So say I have my index.html and I want to include a script in that index.html to call a javascript gateway file I have. I've found a few sources on the net talking about it, but I've tried it a few times and I can't get it figured out.

    This is what I've currently done, but can't figure out why it's not working:

    I include a <SCRIPT SRC="http://mysite.com/gateway.js"></SCRIPT> in the <head> tags of my index.html. Then in my gateway.js I copy and pasted this gateway code:


    Code:
    <script type="text/javascript" src="http://www.cpalead.com/mygateway.php?pub=13343&amp;gateid=MTEwODI%3D"></script>
    However, when I open my index.html in my browser it's not pulling my script from gateway.js file. I'm not to savvy with Javascript, so if anyone knows how to do this, that would be great.

    Thanks!

  2. #2
    Join Date
    Oct 2006
    Posts
    28
    I don't think you can have another <script> tag nested inside a script file you are including from a separate file like that if that is in fact what you are doing. I don't think that the browser will resolve the remote external script file reference contained in the first external script file. It might be a cross-domain scripting problem for one thing.

  3. #3
    Join Date
    Feb 2010
    Posts
    6
    Alright, I understand what you're saying, but can you think of a way to call a script in my page.html from another place, such as gateway.js. That gateway.js is calling a script from another site though, a site that provides me the actual script.

    Hopefully that made sense, maybe someone will have an idea of how to do that.

    Thanks!

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by haydenm92 View Post
    That gateway.js is calling a script from another site though, a site that provides me the actual script.
    A javascript code can not "call a script from another site". I really don't understand how your gateway.js looks like.

  5. #5
    Join Date
    Feb 2010
    Posts
    6
    Alright, it's hard for me to explain what I'm trying to do because I don't know the terminology all that well. Basically I have a javascript code that calls a popup gateway window from an advertising site. The advertising site gives me the instruction to post the code from my OP in the <HEAD> tags of my website.

    Well I have multiple pages I want this javascript code on, and rather than pasting that code in the <HEAD> tags of every single page, I want to put one generic code that pulls the javascript code possibly from a separate file. The reason for this is because if I have to change this javascript code later for testing I will be able to just go edit this separate file and change the code, rather than changing each page because these pages will call the javascript in that file.

    I hope that made sense, if anyone knows how or what I'm trying to do, I would greatly appreciate an idea.

    Thanks!

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    OK. I understood. You want to avoid writing "by hand" similar codes on each document of your site. This is rather to be solved on using server side includes. If you are using an Apache server and you are using php as server-side language, I can show you how to do that. If not, tell us which server-side architecture (server machine, server-side scripting language) do you have at disposal, and I will move this thread to a proper Forum.

  7. #7
    Join Date
    Oct 2006
    Posts
    28
    Going back to the original post. . .If you had to put this:

    <SCRIPT SRC="http://mysite.com/gateway.js"></SCRIPT>

    in every one of your files, how is that any different from having to put this:

    <script type="text/javascript" src="http://www.cpalead.com/mygateway.php?pub=13343&amp;gateid=MTEwODI%3D"></script>

    in every one of your files?

    If I'm understanding correctly what you did you put
    <head>
    <SCRIPT SRC="http://mysite.com/gateway.js"></SCRIPT>
    </head>
    in your index.html file. In the file, gateway.js you have just put as the only content in the file:

    <script type="text/javascript" src="http://www.cpalead.com/mygateway.php?pub=13343&amp;gateid=MTEwODI%3D"></script>

    Is this correct? If so I fail to see the point of the gateway.js.

  8. #8
    Join Date
    Feb 2010
    Posts
    6
    Quote Originally Posted by ahurtt View Post
    Going back to the original post. . .If you had to put this:

    <SCRIPT SRC="http://mysite.com/gateway.js"></SCRIPT>

    in every one of your files, how is that any different from having to put this:

    <script type="text/javascript" src="http://www.cpalead.com/mygateway.php?pub=13343&amp;gateid=MTEwODI%3D"></script>

    in every one of your files?

    If I'm understanding correctly what you did you put
    <head>
    <SCRIPT SRC="http://mysite.com/gateway.js"></SCRIPT>
    </head>
    in your index.html file. In the file, gateway.js you have just put as the only content in the file:

    <script type="text/javascript" src="http://www.cpalead.com/mygateway.php?pub=13343&amp;gateid=MTEwODI%3D"></script>


    Is this correct? If so I fail to see the point of the gateway.js.

    because if I need to update the javascript code, it would be way easier to do it in the gateway.js page so it would reflect on all my pages, rather than update the javascript code on each page.

  9. #9
    Join Date
    Oct 2006
    Posts
    28
    But you have not shown us any actual javascript. What code are you talking about having to modify? All I see is two external javascript file link tags. Are you saying your gateway.js file contains some of your own custom javascript code and additionally you are also trying to put the script tag linking to the other external javascript file in gateway.js? If this is the case it won't work that way. You'd just have to put two sets <javascript src="..."></javascript> tags in each of your html files such that each file links to both external script files.
    Last edited by ahurtt; 02-24-2010 at 09:42 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles