在JavaScript中解析URL [英] Parse an URL in JavaScript
本文介绍了在JavaScript中解析URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用JavaScript解析URL(也使用jQuery)?
How do I parse an URL with JavaScript (also with jQuery)?
例如我在我的字符串中有这个,
For instance I have this in my string,
url = "http://example.com/form_image_edit.php?img_id=33"
我想得到的价值img_id
我知道我可以使用PHP使用 parse_url()
轻松完成此操作,但我想知道如何使用JavaScript。
I know I can do this easily with PHP with parse_url()
, but I want to know how it is possible with JavaScript.
推荐答案
您可以使用创建 a
元素的技巧,添加网址,然后使用其位置对象。
You can use a trick of creating an a
-element, add the url to it, and then use its Location object.
function parseUrl( url ) {
var a = document.createElement('a');
a.href = url;
return a;
}
parseUrl('http://example.com/form_image_edit.php?img_id=33').search
将输出:?img_id = 33
您还可以使用 php.js 获取 JavaScript中的parse_url函数。
我建议使用优秀的 URI.js 库,如果你需要做的不仅仅是超级简单的URL处理。
I would recommend using the excellent URI.js library if you need to do anything more than super simple URL handling.
这篇关于在JavaScript中解析URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文