0

So i have been able to catch in separate tests catching url parameters successfully. But i'm struggling with connecting it all up.

Where the parameter is in a second layer. If i can put the pubID in a var that would be great.

Or if somebody can point me in the right direction that would be great. Thanks

So i'm planning to build api widgets, but having trouble with getting parameter from the script

On the clients server i would have the following script

<script src="http://api.yoursite.com/js/loader.js?pubID=test123" type="text/javascript"></script>
<div id="myDiv"></div>

Then the loader script would have the following code.

The following code is the loader.js file. This code has been written by someone else which works great for widgets, but having trouble to get the basics done haha

(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.7.2') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
    "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
if (script_tag.readyState) {
  script_tag.onreadystatechange = function () { // For old versions of IE
      if (this.readyState == 'complete' || this.readyState == 'loaded') {
          scriptLoadHandler();
      }
  };
} else {
  script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main(); 
}


/******** Our main function ********/
function main() { 
jQuery(document).ready(function($) { 
    /******* Load CSS *******/
    var css_link = $("<link>", { 
        rel: "stylesheet", 
        type: "text/css", 
        href: "style.css" 
    });
    css_link.appendTo('head'); 



    $.getJSON("http://api.yoursite.com/js/json.php?jsoncallback=?",
function(data){
  $('#myDiv').html(data.name);  
});

});
}

})(); // We call our anonymous function immediately

To catch the parameters i used the following working script.

// Parse URL Queries
function url_query( query ) {
query = query.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var expr = "[\\?&]"+query+"=([^&#]*)";
var regex = new RegExp( expr );
var results = regex.exec( window.location.href );
if( results !== null ) {
    return results[1];
    return decodeURIComponent(results[1].replace(/\+/g, " "));
} else {
    return false;
}
}

// Example usage - http://www.kevinleary.net/?load=yes
var url_param = url_query('pubID');
if( url_param ) {
alert(url_param); // "yes"
}
SmileyWar
  • 25
  • 1
  • 9

1 Answers1

0

It's called query string.

Use the function provided in this post: How can I get query string values in JavaScript?

Community
  • 1
  • 1
darksky
  • 1,955
  • 16
  • 28