Can I use jQuery to do $_GET method/look at file's url [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-11 01:19:55

问题


Possible Duplicate:
Get query string values in JavaScript

I am writing a jQuery function that saves an item remotely with Ajax. I'd like to be able to grab the edit.php?id=###&field=### info from the url right in my function. How would I do that?


回答1:


You don't need jQuery for this task, you can do it simply with javascript :

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
     return "";
  else
    return results[1];
}

Then if you retrieve this url : http://www.yoursite.com/Index.php?g=12&param=43 to retrieve the 'param' parameter, all you need to do is this :

var param_value = gup('param');

or the 'g' parameter like this :

var param_value = gup('g');

or switch 'param' with the name of the parameter you need.



来源:https://stackoverflow.com/questions/7234678/can-i-use-jquery-to-do-get-method-look-at-files-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!