Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function calls and var pass through

    **Edited for a typo and to clarify my problem.

    I'm working on a website with multiple .js files. I'm trying to make a script that calls from one external script to another. I'm looking to make a small script that will allow a one stop shop for changing where an href points on multiple pages. The code is like this.

    Webpage

    <head>
    <script src="/siteroot/script01.js" type="text/javascript"></script>
    <script src="script02.js" type="text/javascript"></script>
    <head>
    <body>
    <a href="#" onclick="linkSite('page_one')">Click here for Page One</a>
    <a href="#" onclick="linkSite('page_two')">Click here for Page Two</a>
    </body>

    Script one is the function to change the page. Standard for all sites.

    Script01

    function linkSite (ind_var)
    {
    callLink(ind_var)

    window.location=pass_var
    }

    Script two sets up where the hrefs point. This will be different per site.

    function callLink (p_value)
    {
    //Do not modify above this line
    //Place the URL for the site link in the quotes for the proper page.

    page_one="/siteroot/page01/webpage.html"
    page_two="/siteroot/page02/webpage.html"

    //Do not modify below this line

    if (p_value=="page_one")
    {
    pass_var=page_one
    }

    else if (p_value=="page_two")
    {
    pass_var=page_two
    }

    else //If the link text does not match return error.
    {
    alert ("Invalid Site Link")

    pass_var="#"
    }

    return (pass_var)
    }

    I apparently cannot make calls to the callLink in script02.js from linkSite in script01.js, nor can I call to callLink if I embed it inline on the page in the header. In both cases I get an 'object expected' error. I belive it's because it's not finding the 'callLink' function. If I put both functions in script01, it functions properly. Here are my questions:

    Is there a way around this limitation? Even if I'm doing this poorly in this case, I'd like to know if there is a way to do this reguardless for future referance.

    Am I makeing an uneccesary script here? Or a poorly written one? Let me know if there's a better way to do this. Remember that the variables will change for each site within the hierarchy.

    Thank you greatly for your assistance in advance.

    AhmNee
    Last edited by AhmNee; 05-24-2006 at 10:57 PM.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    this line
    return (pass_var)
    looks usless and incorrect to me . Try again after removing it.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    You most likely need to include them in the order of their depencies, so if script02 needs somethign from script01, include script01 first, then script02. Don't have circular dependencies where script01 needs something from script02 and script02 needs something from script01.

    Once the functions and variables are defined in one script, they are available to all subsequent scripts on the page, so you shouldn't have any problems, unless your scripts have syntax errors in them and aren't properly loaded.

    I always advise debugging javascript in FireFox because the javascript console provides better error messages than IE. Unless you have an option turned on in IE, you won't get error messages every time a script error occurs.

  • #4
    New to the CF scene
    Join Date
    May 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    return (pass_var)

    Kor:
    Am I wrong in my understanding the scope of a variable in javascript? I was under the impression that the 'pass_var' which was defined by the callLink function would go out of scope as soon as callLink ran it's function. Thus I would need to return a value to the function that called callLink in the first place.

    In other words, when callLink ran pass_var would cease to be once you reached where the return function is. Thus siteLink would error with 'object expected' when it went to read it's value because the variable went out of scope at the end of callLink.

    Beagle:
    I've had the script files in the flow both directions. That occured to me as well. But these are called functions. They should both have been loaded by the time the onclick happens.

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Ahhh, I see what you're doing now.

    You're right, once the function finishes executing, the local vars disappear.

    But that's true whether or not you return the value.

    So, in the scope of linkSite, pass_var is undefined.

    What you need is:

    var retVal = callLink(ind_var)

    window.location=retVal;

    Or just:

    window.location = callLink(ind_var);

  • #6
    New to the CF scene
    Join Date
    May 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting the picture now

    Beagle:
    Alright. I hadn't thought of using "window.location = callLink(ind_var);" I'll try that. With the two functions in the same .js file, I have gotten the correct link by adding "alert (pass_var)" just above the window.location string. Not to argue. You've definitely shown me a better way to setup the code.

  • #7
    New to the CF scene
    Join Date
    May 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Here's what I'm using now.

    Thanks again, everyone. This is the script I'll be using in case this can help someone. I still don't know why I couldn't get the variables to pass between the .js scripts. I'll test it and get back to the board if I come up with answers.

    On the website/s:

    <a href="#" onclick="window.location = callLink('page01');">
    Google</a><br>
    <a href="#" onclick="window.location = callLink('page02');">
    Yahoo</a><br>
    <a href="#" onclick="window.location = callLink('page03');">
    Lycos</a><br>
    <a href="#" onclick="window.location = callLink('page04');">
    Ask.com</a><br>
    <a href="#" onclick="window.location = callLink('page05');">
    Patriot Search</a><br><br><br>

    In the .js file:

    function callLink (p_value)
    {
    var _mk = 0
    var s_link = new Array ()

    //Do not modify above this line

    /*

    Instructions:

    Place the URL for the site inside the quotes under the proper page title.
    For example, the link URL for contact us would go underneath like this:

    s_link [?] = 'contact us'
    s_link [?] = 'http://link.to.contact_us/URL/contact_us.asp'

    */

    s_link [0] = 'page01'
    s_link [1] = 'http://www.google.com'
    s_link [2] = 'page02'
    s_link [3] = 'http://www.yahoo.com'
    s_link [4] = 'page03'
    s_link [5] = 'http://www.lycos.com'
    s_link [6] = 'page04'
    s_link [7] = 'http://www.ask.com'
    s_link [8] = 'page05'
    s_link [9] = 'http://blog.outer-court.com/patriot/'

    //Do not modify below this line

    while (p_value != s_link [ _mk ])
    { _mk = _mk + 2
    }

    _mk = _mk + 1


    s_value = s_link [ _mk ]

    return (s_value)
    }

    The code works like a charm and hopfully I've commented enough so the unadept can change the URLs in the .js without hosing the code.

    Thanks again.

    AhmNee

  • #8
    New to the CF scene
    Join Date
    May 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting the picture now

    ** Double Post
    Last edited by AhmNee; 05-25-2006 at 11:25 PM.


  •  

    Posting Permissions

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