Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从一个页面调用javascript函数在第二个页面上添加元素_Javascript_Jquery_Html - Fatal编程技术网

从一个页面调用javascript函数在第二个页面上添加元素

从一个页面调用javascript函数在第二个页面上添加元素,javascript,jquery,html,Javascript,Jquery,Html,我试图从一个页面调用javascript函数,以便在另一个页面中添加元素。正在调用该函数,但问题是,当我将其重定向到新页面时,之后的代码不会执行 window.func = function() { location.href = 'second.html'; var div = document.getElementById("div1"); var button = document.createElement("button"); div.appendChild(button); }

我试图从一个页面调用javascript函数,以便在另一个页面中添加元素。正在调用该函数,但问题是,当我将其重定向到新页面时,之后的代码不会执行

window.func = function() {

location.href = 'second.html';
var div = document.getElementById("div1");
var button = document.createElement("button");
div.appendChild(button);
}

此函数由第一页调用,重定向后的更改应在第二页中进行。我不能对page2使用document.ready函数,因为我不希望每次加载page2时都发生更改,而是希望更改仅在按下page1中的按钮后发生。

您需要通过传递某种标志(即通过URL参数)在第二页中执行代码,并在加载第二页时检查是否存在值然后执行功能

,这样经过大量的尝试和尝试,我就能够实现我想要的。我从一个页面重定向到另一个页面,但在第二个页面的url中添加了一个字符串

function myFunction() {

window.location.href = "html2.html?sound";
}
然后在第二个页面中,我仅在url包含某个单词时调用该函数。 函数myFunc2(){


每次加载页面时都会调用该函数,但仅当url包含“声音”时才添加按钮word。

重定向到另一个页面会终止所有JavaScript。之后编写的代码将针对当前页面。不是针对重定向用户所在的页面。也许这个问题会对您有所帮助。我知道它会终止它,但我不希望在加载第二个页面时立即发生更改。我希望更改仅在按钮被预加载后发生从page1Cant中选择相同的变量还是在html消息中发送变量?
var url = document.URL;
url = url.split('?');
console.log(url[1]);
var temp = url[1];
if(temp == "sound") {
    console.log("sada");
    var button  = document.createElement("button");
    button.innerHTML = "hello";
    document.getElementById("div1").appendChild(button);
 }
}