javascript - 重定向到特定页面并在js中编写该页面的代码onload

标签 javascript html jquery jquery-events

在重定向一页时,应该执行一些代码,但这是不可能的

if (something === true) {
  /* Redirect to someotherpage.php */  
  window.location.href = "someotherpage.php"  
  var test = "someotherpage.php";  

  /* onload of someotherpage.php, execute this introjs step */  
  $(test).load(function() {  
    if (window.location.href === test) {  
      /* unable to execute this */  
      introJs().addStep({  
        element: $('#ck-dashboard-pending-task')[0],  
        intro: 'Here you can see the todo list',  
        position: 'left'  
      }).setOption('scrollToElement', true).start();  
    }  
  });  
}  

最佳答案

您的问题是,当加载另一个页面时,您试图在页面内执行代码。您不能以这种方式执行此操作(您可以在单页应用程序中执行此操作,但那是另一个故事)。

在您的 someotherpage.php 中,您应该添加以下代码:

$(window).load(function(){  
      introJs().addStep({  
      element:$('#ck-dashboard-pending-task')[0],  
      intro:'Here you can see the todo list',  
      position:'left'  
      }).setOption('scrollToElement', true).start();
  });  

您应该删除页面中的这部分代码: var test = "someotherpage.php";

/* onload of someotherpage.php, execute this introjs step */  
  $(test).load(function() {  
    if (window.location.href === test) {  
      /* unable to execute this */  
      introJs().addStep({  
        element: $('#ck-dashboard-pending-task')[0],  
        intro: 'Here you can see the todo list',  
        position: 'left'  
      }).setOption('scrollToElement', true).start();  
    }  
  }); 

编辑: 根据OP的要求,这里有一个澄清: 在您的第一页中,您可以执行以下操作:

if (something === true) {
  /* Redirect to someotherpage.php */  
  window.location.href = "someotherpage.php?some_condition_you_want_to_check=1"
}  

someotherpage.php上:

$(window).load(function(){  
    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results==null){
           return null;
        }
        else{
           return results[1] || 0;
        }
    }

    if($.urlParam('some_condition_you_want_to_check') !== null){
        introJs().addStep({  
            element:$('#ck-dashboard-pending-task')[0],  
            intro:'Here you can see the todo list',  
            position:'left'  
            }).setOption('scrollToElement', true).start();
        });  
    }
  }

当然,我的代码是未经测试且肯定有待改进的部分代码,但这应该会让您了解需要做什么才能实现您想要的目标。

关于javascript - 重定向到特定页面并在js中编写该页面的代码onload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386459/

相关文章:

javascript - 如何等待子组件任务完成后再继续

javascript - 将输入值输出到范围

javascript - 在对象创建器中向 DOM 添加选项

Javascript OOP 和 jQuery 无法触及对象属性

javascript - 如何将 URL 参数添加到给定 href 中数据属性的 URL?

javascript - Chart.js - 在不使用工具提示的情况下向气泡图元素添加文本/标签?

javascript - 使用 AngularJS 进行子域路由

javascript - 如何从 "View in iTunes"这样的浏览器启动Windows应用程序?

html - 清除 HTML 标记中的所有内联事件

javascript - jQuery AJAX php 'post' 返回实际的 PHP 代码