www.webdeveloper.com
Results 1 to 3 of 3

Thread: Accessing jQuery Variable Earlier In Page

  1. #1
    Join Date
    Jun 2008
    Location
    Bend, Oregon
    Posts
    8

    Accessing jQuery Variable Earlier In Page

    Hello there,

    I'm a jQuery newbie (and not really a stud at JS, either), so bear with me. We have a web site provided by the software company that created our vacation rental management software, so, as such, I don't have entire control over the pages -- just bits and pieces. They recently started including jQuery 1.4.4 along with jqueryUI and cookie plugin on the pages and utilizing it a bit (as well as a custom jquery bit of code they wrote up). My question is thus, and here's the relevant code (that I have control over):

    HTML Code:
    <script type="text/javascript">
    var phoneflat = $.util.getCampaignPhone('MCPH').replace(/[^0-9]/g, '');
    if(phoneflat=="null"){var DNISPh='88812345678'}
    else{var DNISPh=phoneflat}
    var TalkURL='http:\/\/www.blahblah.com/blah/?DNIS='+DNISPh;
    </script>
    
    <a href="http://blahblah.com" onclick="javascript:window.open(TalkURL,'Talk','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=630,height=440'); return false;">Click Me!</a>
    Basically, we have a phone-number tracking system on our Web site, and the toll-free number on our site gets replaced depending on how the MCPH cookie is set. The above code pulls formatted number from that campaign cookie, removes the formatting from the number, and sets the pop-up URL to include the number, passing the number after the "?DNIS=" (which is required for this specific campaign tracking to work properly).

    This all works fine and dandy if the jquery and cookie code is all before the above code snippet on the page, but the developers of the software are trying to follow best practices and are loading all the javascript files at the end of the page, and this is far earlier in the page. So there's really no way for me to pull that ".util.getCampaignPhone('MCPH')" as the cookie may not have been set yet, nor is the campaign data ready.

    Any ideas to be able to get that variable somehow or is there something I'm missing? The folks at the software company are trying to wrap their head around it, too, but I figured I'd ask here as well :-)

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    easy, just tell it to wait for jQuery:
    Code:
    (function wait4jq(){   if(!window.jQuery){ return setTimeout(wait4jq, 30); }
    
       var DNISPh= $.util.getCampaignPhone('MCPH').replace(/[^0-9]/g, '') || '88812345678' ;
       //removed "var " below to make TalkURL a global:
       TalkURL='http:\/\/www.blahblah.com/blah/?DNIS='+DNISPh;
    
    }()); //end delay wrapper function
    the top and bottom lines are new, the middle has been cleaned up.
    Last edited by rnd me; 02-04-2011 at 03:37 AM.

  3. #3
    Join Date
    Jun 2008
    Location
    Bend, Oregon
    Posts
    8
    Quote Originally Posted by rnd me View Post
    easy, just tell it to wait for jQuery:
    Code:
    (function wait4jq(){   if(!window.jQuery){ return setTimeout(wait4jq, 30); }
    
       var DNISPh= $.util.getCampaignPhone('MCPH').replace(/[^0-9]/g, '') || '88812345678' ;
       //removed "var " below to make TalkURL a global:
       TalkURL='http:\/\/www.blahblah.com/blah/?DNIS='+DNISPh;
    
    }()); //end delay wrapper function
    the top and bottom lines are new, the middle has been cleaned up.
    Perfect! Just what I was looking for -- thanks! Our software provider had already come up with another solution, but I tested this one and it works, too.

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