0

I'm having a very simple problem understanding the scope of variables with external javascript pages. From what I read if I call an external sheet all global functions and variables should be able to be accessed. I can't seem to get it to work. Knowing the right question to ask to search is the hard part and I could comb through the internet for days for a problem someone could probably explain easily and quickly. I have seen something simliar in this tutorial http://www.tizag.com/javascriptT/javascriptexternal.php, I searched quickly stack found this: What is the scope of variables in JavaScript? and even as I write this I'm looking for the answer.

Simply I have an external sheet in the same directory and the file name is trial.js. Then I have an index.html simply trying to use the global function from the external trial.js to the index page. This is possible? according to the tizag tutorial using a function in the button onclick attribute. Also many frameworks work on this principle too right? Although I know they are a bit more sophisticated using $(a)<~~~references?? or something. Thanks for anyone's help I'll continue to look but hopefully the internet can help!!

localhost/trial.js:

function trySomething(){
    alert("trying");
}

localhost/index.html:

<html>
<head>
<title>Objects222</title>
<script src="trial.js"></script>
</head>
<body>
    <script type="text/javascript">
        trySomething();
    </script>
    Hello
</body>
</html>

It was originally in resource but I wanted to make it even simplier. using firefox and chrome doesn't work for me??? Those who say it's working upon loading the page you get an alert that says "trying" ? Anyone any ideas why it might not be working locally. I cleared all my cache and tried renaming to force upload. Also used Chrome, Firefox, and IE still no alert on load @ localhost.

Community
  • 1
  • 1
Roger
  • 107
  • 6
  • 5
    your script source tag is pointing to resource/trial.js, but according to your question, it's located at localhost/trial.js? Is this just a typo? – Krease Jan 08 '13 at 01:10
  • 1
    Check in the network panel of your browser's developer tools whether the file is correctly loaded. If so, it should work. – Bergi Jan 08 '13 at 01:15
  • 1
    No, `$(a)` is no special thing. The `$` is just a global variable with an (odd?) short name, and its value is a function. You could do `$ = trySomething;` and then call `$()` as well. – Bergi Jan 08 '13 at 01:17
  • There is nothing wrong with your example. As per the tutorial, global function is accessible to any script that calls it (albeit the tutorial page uses it in an `onclick` attribute, which is no longer such a popular technique). –  Jan 08 '13 at 01:24
  • Sorry about the /resource/ I meant to make it as simple as possible for the question. I fixed it but still not getting the alert on page load now tried Chrome, Firefox, and IE. – Roger Jan 08 '13 at 02:03

3 Answers3

0

I'm not certain, but I don't think your supposed to have a / before "Resources". ex. "Resources/myscript" would work. Also, if "trial.js" is in the same directory as "index.html" then you don't need "Resources/".

  • the `/` is valid, especially if the script is located at localhost/resource/trial.js; it's just whether or not it's correct for solving this problem. – Krease Jan 08 '13 at 01:16
  • `'Resources'` is not the same as `'resources'` in most usual cases. Even more obviously, `'resource'` is not the same as `'resources'`. –  Jan 08 '13 at 01:20
  • typo on my part I was brought it down to the same folder and checked my call I made the edit's on the page still not working locally but seems to work fine in a published environment. – Roger Jan 08 '13 at 02:12
0

I just tested this on Apache and it works fine, assuming that your external JavaScript file is located within a folder called resource.

Alex W
  • 37,233
  • 13
  • 109
  • 109
0

This seems like it should be fine so I copied your code and tested it and it is working for me. The one change I made was to remove the first slash in the script src because my "resource" folder isn't in the site root. Is your js file definitely being loaded?

Nic Peck
  • 112
  • 1
  • 8
  • It does work here in this test/stack that is awesome. Any ideas why it might not be working locally? – Roger Jan 08 '13 at 02:11
  • I have cleared my javascript history as well as renamed the file to trial1.js and made the necessary changes to the – Roger Jan 08 '13 at 02:14
  • now suddenly working something kicked in. But had you not shown me that it works by offering it in a published stage I would not have forced it through. Seems I will have to become accustomed to no breakpoints and Stack errors to save me... which is daunting. Can't vote or I'd +1 this. THANK YOU! – Roger Jan 08 '13 at 02:21