jos*_*osh 5 javascript url detection
我正在使用以下脚本强制将特定页面(第一次加载时)强制转换为(第三方)iFrame.
<script type="text/javascript">
if(window.top==window) {
location.reload()
} else {
}
</script>
Run Code Online (Sandbox Code Playgroud)
(澄清一下:这个'嵌入'是由第三方系统自动完成的,但只有在页面刷新一次时才会完成 - 因为样式和其他一些原因我从一开始就想要它.)
现在,我想知道这个脚本是否可以通过检测其"父"文档的当前URL以触发特定操作的方式得到增强?假设第三方网站的网址是" http://cgi.site.com/hp/ ...",而iFrame的网址是http://co.siteeps.com/hp/ ... ".是否有可能实现...... 像这样用JS:
<script type="text/javascript">
if(URL is 'http://cgi.site.com/hp/...') {
location.reload()
}
if(URL is 'http://co.siteeps.com/hp/...') {
location.do-not.reload() resp. location.do-nothing()
}
</script>
Run Code Online (Sandbox Code Playgroud)
TIA josh
<script type="text/javascript">
if(/^http:\/\/cgi.site.com\/hp\//.test(window.location)) {
location.reload()
}
if(/^http:\/\/co.siteeps.com\/hp\//.test(window.location)) {
location.do-not.reload() resp. location.do-nothing()
}
</script>
Run Code Online (Sandbox Code Playgroud)
当然,第二个if是多余的,所以你可以简单地这样做:
<script type="text/javascript">
if(/^http:\/\/cgi.site.com\/hp\//.test(window.location)) {
location.reload()
}
</script>
Run Code Online (Sandbox Code Playgroud)
你在这里做的是window.location用正则表达式测试它是否与你想要的URL匹配.
如果要引用父级的URL,可以使用parent.location.href.
根据您的评论,如果您想要做其他事情,您可以执行以下操作:
<script type="text/javascript">
if(/^http:\/\/cgi.site.com\/hp\//.test(window.location)) {
location.reload()
}
else if(/^http:\/\/co.siteeps.com\/hp\//.test(window.location)) {
//do something else
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果你在其他情况下什么都不做,那实际上是一个NOP(没有操作),所以你甚至不需要那里的其他(或其他的),因为它将是一个空块.
| 归档时间: |
|
| 查看次数: |
7608 次 |
| 最近记录: |