仅在一个网页中加载JavaScript
Load JavaScript in only one webpage
我希望JavaScript只能在论坛的一个网页中工作,所以我使用了以下代码:
if (window.location.href.indexOf('/u1') != -1) {
但它也会影响/u11、/u12、/u13、/u14等网页。
如何使代码仅在/u1中工作?
试试这个,我忽略了你使用干净url的事实。
//split url into an array separated by the /
var url = window.location.href.split("/");
//store the page that you want the script to execute on
var rightPage = "u1";
//if the last index of the url array equals the value of the rightPage
//note: -1 is subtracted because array indexes start at 0
if (url[url.length - 1] === rightPage) {//begin if then else
//do something if the your on the rightPage
alert("right");
}
else{
//do something if you are not on rightPage
alert(page.join("/"));
}//end if then else
这个函数会发现url中是否有一个精确的段"u1",但不关心它在href路径中的位置(对于http://somewhere.com/one/two/three/u1/foo/bar),但它很简单,应该足够了。
var segments = window.location.href.split("/");
var found = false;
// You can also do this loop with index and count the position if needed
for (var segment in segments){
if ( segment == "u1" ){
found = true;
break;
}
}
// do with found whatever you need below
相关文章:
- 如何防止网页加载后自动启动功能
- 如何在网页加载时显示加载内容,并在页面完全加载时隐藏此内容
- 如何在网页加载asp.net上显示文本框中第二个字母的光标位置
- 我能得到网页加载的时间吗
- 如何在整个网页加载后执行脚本
- 网页加载不正确:'"JSON”;未定义'错误
- 如何在网页加载之前添加加载页面
- 删除不需要的javascript和css是否包括减轻网页加载时间
- 如何在硒中仅从网页加载html
- 如何获取网页加载时间
- 如何在网页加载之前制作预加载器
- 如何使用网页加载的源从网页获取文本框内容
- Touchstart 事件永远不会在网页加载的首次加载时在 Android Chrome 上触发
- 网页加载完成后图像淡入
- 在网页加载后覆盖现有的javascript方法应该是可能的
- Selenium网页加载问题
- 在整个网页加载后加载谷歌广告
- 使用Javascript在网页加载时自动点击
- 如何在javascript中创建进度条以及如何在网页加载时使用它
- 当网页加载时,自动聚焦HTML表单输入的最佳方法是什么?