SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to Set the Script In Action From an External js file

    Hello, I've created a rather lengthy jQuery script into an area that will appear on multiple pages. The script works just fine when I place it inline but when I've created it into an external js file and reference the script with the <script type="javascript" src="path to file"></script> and removed the inline jQuery, the script no longer works.

    I'm wondering if it is because I have many different functions actually going on with call backs and such in the script. I'm not even sure where to place the "onclick" call onto which element, if at all.

    How do you make the element that you want to be acted upon to call the script into action?

    Thanks for your help.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you adding your script tag in the <head> or before the closing </body> tag? If you're adding it to your <head> tag move it to just before your closing </body> tag as that will ensure it waits until the DOM is ready which sounds like the issue. If you can't do this simply wrap your file in a document ready function like the below.

    Code javascript:
    $(function() {
        // Your code here
    });

  3. #3
    SitePoint Evangelist silver trophybronze trophy
    Join Date
    Jul 2013
    Posts
    406
    Mentioned
    22 Post(s)
    Tagged
    0 Thread(s)
    The right way to call a javascript is:
    Code:
    <script type="text/javascript" src="path to file"></script>
    (and inside the javascript file there are no <script> -tags, just the script-functions theirself).
    Maybe this was it?

  4. #4
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Francky View Post
    The right way to call a javascript is:
    Code:
    <script type="text/javascript" src="path to file"></script>
    (and inside the javascript file there are no <script> -tags, just the script-functions theirself).
    Maybe this was it?
    Yes I definitely placed it like that. I don't have access to the < head > section so I had to place it right before my content begins (in an editing window in EzPublisher). I don't have access to any < body > or <head > tag.

    Isn't there something that I need to place in the element that it works on though? i.e. <div onclick="some script" > to make it work?

    Thanks for helping an newb to JavaScript and jQuery.


Tags for this Thread

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
  •