在加载的特定页面上执行功能 [英] Execute a function on specific page loaded

查看:84
本文介绍了在加载的特定页面上执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想运行一个函数,只有当我在我的网站的特定部分,并且一旦我离开网站的那一部分,我想要退出该功能。我已经试过这样做了。

  window.onload = carousel(); 

但是,一旦网站的任何页面加载,而不是我想要的特定页面 page3.html )。



只能使用html,css和javascript。没有外部库和类似的

解决方案

使用 window.location 。然后将其包装到函数中,并将其分配给 onload 事件: > window.onload = function(){
if(window.location.href.indexOf('page3.html')> -1){
carousel();
}
}


I want to run a function only and only when I'm on a specific part of my website and once I leave that part of the website I want to go out of that function. I've tried to do so with

window.onload = carousel();

But this will happen once the any page of the website loads and not the specific one I want to (page3.html).

Only using html, css and javascript. No external libraries and such

解决方案

Check the url by using window.location. Then wrap that inside a function and assign it to your onload event:

window.onload = function() {
  if (window.location.href.indexOf('page3.html') > -1) {
    carousel();
  }
}

这篇关于在加载的特定页面上执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆