如何使用 jquery 提取 URL 参数
How to extract URL parameters with jquery
我有一些这样的链接:
mywebsite.com/tutos.php?name=tuto_name#comments
mywebsite.com/tutos.php?name=tuto_name#download
我的问题:如何获取#之后的文本。
谢谢。
window.location.hash
是一个跨浏览器解决方案,返回值(包括哈希)
您可以通过执行以下操作来删除哈希:
var hash = window.location.hash.substr(1);
我使用以下方法,因为它不仅获取hash
值(没有哈希本身(取第二部分(split
的array[1]
)),而且还测试了在某些情况下可能导致问题的undefined
情况。
var hashVal = window.location.hash.split("#")[1];
if( hashVal && hashVal != "undefined" ) {
//work it
}
我使用以下JS函数来执行此操作:
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)','i').exec(location.search) || [, ""])[1].replace(/'+/g, '%20')) || null
}
您可以使用
window.location.hash
.它需要#
(即#comments
)。要删除试用#
请使用.substring(1)
。例:
var str = window.location.hash.substring(1);
alert(str);
相关文章:
- 通过Javascript将HTML中的电话号码与URL参数进行交换
- 如何使用Javascript从URL参数中自动填充文本字段
- 如何将Google Analytics配置为将URL参数作为单个页面进行跟踪
- 在url参数javascript中存储键值对列表
- backbone.js-映射标准url参数-使用多个参数
- 将URL参数传递给ui sref
- Javascript从字符串中剥离所有url参数
- Javascript/jQuery获取所有url参数并添加/更改一个参数
- 在页面加载完成之前,使用URL参数运行JavaScript
- 使用Javascript获取URL参数
- 如何在 JavaScript 中解析 URL 参数
- 主干.js将 url 参数传递给集合
- 谷歌应用脚本中的 UrlFetchApp.fetch 删除 url 参数
- 通过javascript将url参数传递到多个不同的href链接中
- 角度 1 - 获取当前 URL 参数
- 使用jquery获取url参数
- 在Javascript代码中插入URL参数
- Angular 2:HTTP Post Request,带有URL参数和正文类型参数
- 限制与AngularJS状态匹配的动态URL参数的最佳方式
- 如何根据URL参数生成包含JS内容的单个HTML文件