dovgqc2648 2012-07-24 19:50
浏览 49

too long

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"
}
  • 写回答

1条回答 默认 最新

  • doushun4666 2012-07-24 20:31
    关注

    It's called query string.

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

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看