External javascript variable problem

QUESTION: Why would an external javascript variable be undefined if I set it equal to all the links in a page?
BACKGROUND: I have a simple image swap function that works when the script is all in the html. But when I tried taking the script to an external javascript file, when I gather all the anchors on the page. The variable that is supposed to refer to the anchors is considered undefined in the middle of the script.
EXAMPLE: I have place an example online. You may check it out. Please.
Thanks!
Message was edited by: UteFanJason

So I checked out that link, and some information on scope. I got the function to work. Check it out.
I did some more reading, and it turns out that the onload event is when the entire DOM has finished loading. (That is according to a book put out a couple of years ago so it could be different now).
This is what I changed the the code to:
function prepare_thumbs() {
    var main_img = document.getElementById('main_img');
    links = document.getElementsByTagName('a');
    for (x=0; x<links.length; x++) {
        links[x].onclick = function() {
            main_img.setAttribute('src', this.getAttribute('id'));
            return false;
window.onload = prepare_thumbs;
Again, thanks for the help!

Similar Messages

  • JSP Session in JavaScript variable problem

    Hi,
    I have a session variable msgSize that is being set by a Java file in the session.
    aRequest.getSession().setAttribute("msgSize", msgSize);It's value is variable depends on record count of recordset.
    In JSP page where I have to assign it's value to a javascript variable. I am doing this a s follows. I have done this in the JSP file.
    <script type="text/javascript" >
    <!--
    function getVal()
    m=${msgSize};
    -->
    </script>First Problem is I am unable to get this value inside a External JS file. It throws error but inside jsp it is working. How can i use this inside External JS?
    Second problem is once the above code executed value never refreshed to new value?
    Means first time it executed it get value 200 next time again i executed the code the real recordset is 105 but still displaying 200. But when I restart the server next time it takes value 105 by itself....
    How I tackle this problem.?
    Regards,

    problem is I am not refreshing the whole JSP page everytime, only refreshing the partial page using ajax call that's why not getting the session new value...
    any suggestions what to do to get new value?

  • Having problem with calling external javascript functions in LV

    Hi,
    I found this VI: http://lavag.org/topic/10304-discuss-calling-external-javascript/, which I believe can be really helpful for my work. The problem is I can't get it to run based on the following problem:
     - Apparently the "ScriptControl" cannot be loaded
    I am using LabView 8.6, and this vi has been produced in a 8.0 version. Do you think this is the problem? If not, what could cause this problem running this VI?
    Thanks,
    Diogo

    Hi,
    To start I am using this examplo from the google code page.
    <!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>Google Maps JavaScript API Example</title>
        <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
                type="text/javascript"></script>
        <script type="text/javascript">
        function initialize() {
          if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(37.4419, -122.1419), 13);
            map.setUIToDefault();
        </script>
      </head>
      <body onload="initialize()" onunload="GUnload()">
        <div id="map_canvas" style="width: 500px; height: 300px"></div>
      </body>
    </html>  
    It loads a basic map, and is saved in a html file created for example in Notepad. Basically as I said the VI is very simple, see next image.
    But like I said, this strategy does not allow labview to interface dinamically with the javasscript code in the html file. Doing it this way, I would have to constantly update the code in the file with new coordinates and loading the map again. This causes the mentioned flickering problem that I also had with static maps.
    Diogo

  • Webkit. How to connect external javascript from dir C:

    I can't execute javascript from dir C:
    I have some html, that I load in Webkit with loadContent(). In that html invoked function test(). Function contains in a separate javascript file.
    <head>     
    <script language='javascript' type='text/javascript' src="file:///C:/temp/test.js"></script>
    <script>
         function getText() { var val = document.getElementById('text_area').value;
              prompt('getText', "val");
    </script>          
    </head>
    <body>
           <textarea id='text_area' name='text_area' style="width:100%; height:100%"></textarea>     
           <script> test(); </script>
    </body>External javascript (test.js)
    function test() {
            alert('It works!');
         getText();           
    }External js doesn't work. PromptHandler (AlertHandler) doesn't catch anything. If I use wEngine.executeScript('test'), appear error: netscape.javascript.JSException: ReferenceError: Can't find variable: getText.
    But that code works on any other browsers.

    Thank you for the problem report!
    This looks like a bug in WebView. A Jira issue has been filed to track this: http://javafx-jira.kenai.com/browse/RT-17330. Note that the issue may end up classified as not-a-bug because the ability to run local scripts from dynamic contents may be considered a security hole.
    As a workaround, try saving your HTML into a temporary file and load it using WebEngine.load(). It should work as in any other browser. Another option is to include all necessary code in the HTML document loaded with WebEngine.loadContent(), but I gather that is undesirable in your case.
    Edited by: 890759 on Oct 11, 2011 5:19 AM
    Edited by: 890759 on Oct 11, 2011 5:21 AM

  • Assigning a java value(filepath) to a javascript variable

    I have a JSP where I have a java variable(obtained from session) which is nothing but a filepath(say C:\superb.xpdl). When I try to use the variable in javascript using var scriptVar = "<%= sessionVar %>";, the sessionVar value i.e. the filepath is getting modified (like C:superb.xpdl). Slashes are interpredted in a different way(\t for tab, \n for new line etc.,) How to get the filepath with no modifications while assigning to javascript variable? will encoding(using escape()) and decoding help?

    unfortunately you need to double up the slashes
    maybe this will work
    scriptVar = "<%= sessionVar.replaceAll("\\", "\\\\") %>";
    or maybe it will cause a infinite recursion problem (i'm too lazy to try it)

  • How to assign Javascript variable to ABAP variable

    Hi All,
    I have a requirement to create a page where each of the table cells will have drop down boxes with Red, Yellow and Green colors.
    When the user selects the color it has to be saved to database.
    Now the problem is, I'm using HTML as HTMLB was very complicated for the kind of layout the client needed.
    I have created a onChange event for each of these drop down boxes (please keep in mind the dropdown ids are dynamically created by looping the ABAP internal table).
    I'm passing this.id on change event and capturing the id name. Now using this id I have to read an internal table.
    But the problem is, this is javascript variable and cannot be passed directly to read table statement.
    Please help me in this regard.
    Lots of points will be given for valuable inputs.
    PS: I have gone through the thread Assign a Javascript variable value to a ABAP variable
    But doesn't seem to work for my case as I'm using HTML.

    Hi Chait,
    you need to populate a HTML form field with the variable. Most commonly people use the...
    <INPUT type="hidden" id="param1">
    construct.
    In your javascript you can then just set the value of param1.
    Of course you could also dynamically create the elements at runtime in your javascript code.
    Cheers
    Graham Robbo

  • How to access javascript variable's value in a jsp tag?

    Is there any way to access the value of a javascript variable in jsp?

    Hi
    by the way your question is ambigous, i understand the question to some extent here i there is a sample code which may be help full to you. As this problem is faced by me when i was developing some dynamic pages in my company.
    <%@ page language="java" %>
    <html>
    <head>
    <script language="JavaScript">
    var i = 10;
    var k = 20;
    </script>
    </head>
    <%
         String Jspi = "";
         Jspi = ("<script>document.write(i);</script>");
         out.println(Jspi);
         Jspi = ("<script>document.write(k);</script>");
         out.println(Jspi);
    %>
    </body>
    </html>
    This will help u out.
    Cheers
    Rambee

  • How to use javascript variable in BSP/ABAP and vice versa

    Hello Experts,
    In IC WEBCLIENT, there is a view.In the view.htm i want to fetch  the screen resolution and as per the resolution i want set the width of that view.
    1, I wrote the below  javascript to fetch the screen resolution.
       Can i use a notmal ABAP variable inside the javascript? and   If i use can i access that variable after the javascript.?
       Can i assign a javascript variable to a nrmal abap variable??
       The code sample  is like :
    Start of javascript
       if (screen,width == 1024 )
         lv_width = 500  "lv_width is an ABAP variable.
      else
        lv_width = 700
    **END OF JAVASCRIPT
       **Now i want to use lv_width  to assign to a table view attribute(Width)
    <crmc: tableview..
                    width = <%= lv_width >
    the code is not giving any error. But the problem is that lv_width is not giving the correct value, its getting initialized to initial value...
    Requesting the experts to please provide some suggestions/solution for the above issue.
    Thanks and Regards
    Sudhansu

    Hello Sudhansu,
    First of all there was a little mistake at my previous answer:
    you got lw_width = 700
    because at Server Side you have following code:
    lw_width = 500.
    lw_width = 700.
    but if you were writing your code like this:
    <%=lw_width %> = 700.
    then you will get the result that I was talking about : 0 = 700
    Sorry for the mistake
    Now for your question:
    if you want to get a value from the server and assign it to JScript variable you should write like this :
    var aaa;
    aaa = <%=lw_width %>;
    this code assign the server variable lw_width value into aaa  JScript varible.
    I'm not sure what exactly you want to do but look at this link:
    http://www.pageresource.com/jscript/jscreen.htm
    to get the idea how to get and use Screen Resolution variables at your JScript code.
    Good luck
    Eli Steklov
    Please Reward Points if it Helped

  • How to  include external javascript and css resources to a jsf?

    I need to include external css and javascript files to my jsf page. Using the code below does not work because all the styles on the css page are ignored and the functions within the js files are not executed.
    <link rel="stylesheet" type="text/css" href="/resources/styles/style.css" />
    <script language="JavaScript" src="/resources/javascript/util.js"></script>When I try to execute any js function within a script tag on a jsf page I get an object expected error.
    I googled about my problem and found this piece of code for importing styles for an external css.
    <style type="text/css">
    @import url("/resources/styles/style.css");
    </style>How could I add an external javascript file to my jsf?

    Used the relative urls in my first post as well as these, and both were not working.
    <link rel="stylesheet" type="text/css" href="./resources/styles/style.css" />
    <script language="JavaScript" src="./resources/javascript/util.js"></script>The ./ tells that the external file can be found by starting at the current directory and working down the path to the specified file. This is my directory structure. Index.jsf is the file that references the external js and css files.
    -Webcontent
    |_index.jsf
    |_resources
    |__styles
    | |__style.css
    |__javascript
    |__util.js
    Either way I still can't reference the files that I need. Calling a javascript function results in an object expected error, and the styles are ignored.
    If I include the js file directly in the jsf then I can get it to work. I don't want to do this because I have created a handful of seperate js files, and do not want to include all of them in one page. I am using myfaces and tomahawk if that helps.

  • Loading an external javascript with different charset with the page with Firefox

    My page is in UTF-8 while my external javascript file is in UTF-16. I am trying to load it with the html code <script src="x" charset="utf-16"></script>, but eventually Firefox returns with error and indicating that there are illegal characters in the javascript, which is encoded as UTF-8.
    Anyone has similar experience? thx in advanced.

    My problem has been solved. Regards

  • Javascript Variables on Form Login using HttpClient

    Hi,
    I am currently using HttpClient to log onto a forum. As of now I am successfully able to submit a form, which authenticates me, and then takes me to a redirect page(where it welcomes me, stating my username) and then redirects me to the index. On inspecting the code, I saw that some javascript variables are set based on the cookies. However, the httpclient is unable to do this redirection on its own and hence the javascript variables are not getting set and when I subsequently open a new get connection to the index it does not have those javascript variables set and I lose the session.
    I hope I've made my problem clear. Is there any way I could set those javascript variables myself?
    Thanks.

    nikhilm wrote:
    some javascript variables are set based on the cookiesThen just send those cookies along with the request. The session is usually maintained through a cookie with some unique identifier. You should have retrieved this cookie in the response of the login request.

  • Get javascript variable in JSP

    Hi all,
    can anybody tell me- how to get javascript variable in JSP,
    in the same file (JSP).
    Is this possible.
    I can use JavaScript to get the value,
    but then I have no way to get that to the JSP code.
    Please give the appripriate answer with code.
    Thanks
    amitindia

    Hi all,
    Thanks for your quick response.
    So u are right that we can not call javascript variable
    in the same JSP. and pass them to the bean class.
    So what I did --- I get java script variable in another JSP using
    request.getParameter(); and then redirected to previous JSP.
    and I have solve my problem.
    Actually I had to call bean class and pass varible to the bean
    method but I was getting the same problem that I disscussed but now it
    has been solved.
    Thanks again for participation
    amitindia

  • Without using applets geting the output into a javascript variable

    Hi,
    I'm not using executeCommand applets in my webpage,but with the url
    i'm running the transaction. I'm passing the url to iframe as a <b>src</b> in the same page itself. like
    <b>document.getElementById("<iframe>").src="http://localhost/Lighthammer/Runner?Transaction=<Transaction_Name>&<Input_params>=<value>&OutputParameter=*"</b>. It will take 10 seconds to execute the transaction and load into the iframe.
    So I'm giving the
    <b>SetTimeout("alert(document.getElementById('<iframe>').docuemnt.body.innerHTML",10000);</b>
    In the iframe i'm able to see the output as "<u>oktransaction executed</u>" in <u>xml format</u>.
    But in the alert I'm not able to see the same content. <i>In the alert I'm able to see</i>
    <u><iframe id="<id of iframe>" src="<Which i set in the script>"></iframe></u>
    Help me to get the OutputParameter value into a javascript variable.

    Rakesh,
    A couple of observations:
    1.  Why are you calling Runner directly?  You should be using an XacuteQuery template, and reference "/Lighthammer/Illuminator?QueryTemplate=Folder/TRX" syntax when changing the URL in the iFrame.
    2.  You are also using "localhost" which is not portable and may also be causing your problem since your parent page will be based upon the server path, whereas you are forcing the browser to load localhost content into the iFrame (which unless you are physically sitting on the xMII server itself will never work).
    Regards,
    Jeremy Good

  • Accesing externally loaded variables

    i would greatly appreciate a pointer or two as i am having a
    painful transition to AS2.0
    an 8k zip test is at: www.cyan-animatica.com/v2/exttest.zip
    //(wasn`t there an option back in macromedia days to attach a
    file?)
    i am having a poblem accesing externally loaded variables :
    A.--------------------------------------------------------------------
    the external .txt has folowing content:
    &a1_0=hello&a1_1=bye&a1_2=whatever
    B.--------------------------------------------------------------------
    loaded on the first frame thus:
    var thumbs:LoadVars = new LoadVars();
    thumbs.onLoad = function(success:Boolean):Void {
    if (success) {
    trace("LoadVars loaded successfully: "+this.loaded);
    } else {
    trace("Error");
    thumbs.load("my_externals.txt");
    C.--------------------------------------------------------------------
    if want to access one of 3 loaded variable hardcoding, this
    works just fine:
    trace(_root.thumbs.a1_1) //bye
    BUT i need to do this based on the mc/buton name that calls
    it:
    on (press) {
    var kk:String = _name;
    trace(_root.thumbs.kk);
    i get UNDEFINED...obviously i am mixing datatypes if not
    worse
    pc win 2000
    flash prof for flash8 player[

    thanks for a prompt reply, blemmo.
    you write:<<to ADD vars to the LoadVars object, it has
    to be referenced correctly
    what i want to do is READ vars from LoadVars object and
    referencing is exactly what is killing me
    sorry bout the lack of clarity. the zip would help
    (www.cyan-animatica.com/v2/exttest.zip)
    i am loading 3 variables into an object var thumbs:LoadVars
    from one external .txt file:
    a1_0=hello
    a1_1=bye
    a1_2=whatever
    i want to see the contens of let us say the second variable
    trace(_root.thumbs.a1_1);
    produces bye , perfectly - this is exactly what i want, but i
    want it based on a constructed reference
    so, i have button in an mc named "a1_1", the button script
    is:
    on (press) {
    var kk:String = _name;
    trace(kk); // produces a1_1
    trace(_root.thumbs.kk); //this should be same as
    _root.thumbs.a1_1 and produce the same result, but does not
    i suspect that the problem is that kk is declared as string
    datatype, but no matter what i do i cannot get a result
    thanks for your help

  • External javascript for validation

    i have created a registeration form. and need to do validation for the form. but the problem now is that i need to create a external javascipt for valiation. and this scipt must be able to reuse on other form as well.
    can anyone please help me on that? teach me how to create one?

    To create an external JavaScript simply write the JavaScript in a text file and save the file with a .js extension.
    In the HTML reference the file with
    <Script Language="JavaScript" src="myFile.js"></Script>
    where "myFile.js" is the relative path and file name of your .js file.
    To reuse the same validation .js file in different pages you'll either have to :
    1: Pass the form elements being validated in as parameters to functions
    2: Make sure that the HTML form and all of the form elements have the same
    Here is a good link to working with JavaScript:
    http://developer.netscape.com/docs/manuals/js/client/jsguide/index.htm
    name or are in the same order in the DOM.

Maybe you are looking for

  • How do I get the camera widget to re-appear on my I phone 4S

    The Camera Widget on my I Phone 4S is no longer on the screen--everything else is working properly.--the cameras are working as I can use FaceTime.  Have been unable to figure out how to get the widget to reappear so I can take photos.

  • How to use external jar in a program

    I am writing a java program which requires The class org.apache.commons.lang.StringUtils and the method StringUtils.upperCase(). I have downloaded the commons-lang-2.1.jar jar and put it in the lib directory under my project directory. But while comp

  • IPhone 4 (Orange UK)

    Well, I've made a mistake of buying a 3gs from Orange on May 2th. On a side note... Orange doesn't have any return policy! After using the Orange website, it's acknowledged that I'm not eligible to upgrade, obviously. But it's mentioned that I can bu

  • ITunes update corrupt

    The recent iTunes update is corrupt!  do no install!!

  • Physical Memory Dump in Windows 7

    I bought my computer a little over a year ago, an HP Pavillion 6313w, at Wal-Mart. Up until the past week, I started receiving an error when completing a full scan using the Mcafee program that came with Verizon Internet Security Suite about dumping