admin管理员组文章数量:1323335
I am currently building a google extension and this is what I'm going for:
Do something on awesome.page1.html then automatically press Next Page. When the new page has loaded, fill in a form's textfield.
My issue is that even though I know how to fill in the textfield, I don't quite know how to tell it to fill it in once page2 has loaded. It keeps trying to do everything on page 1.
This is what I'm trying but it's not working:
function addEffect() {
document.getElementsByClassName("effect1 shine")[0].click();
document.getElementsByClassName("nextPage BigButton")[0].click();
nextStep();
}
function nextStep() {
if(document.getElementsByClassName("myformTextField imageName") != undefined) {
alert('Page 2 is up.');
}
else {
alert('Page 1 is still up.');
setTimeout("nextStep()", 250);
}
}
I'm using an alert just for testing, and I keep getting the "Page 2 is up" even though it is still on page 1. I'm checking if an element, which is only present in page 2, is up. How could I make sure page2 is up?
I am currently building a google extension and this is what I'm going for:
Do something on awesome.page1.html then automatically press Next Page. When the new page has loaded, fill in a form's textfield.
My issue is that even though I know how to fill in the textfield, I don't quite know how to tell it to fill it in once page2 has loaded. It keeps trying to do everything on page 1.
This is what I'm trying but it's not working:
function addEffect() {
document.getElementsByClassName("effect1 shine")[0].click();
document.getElementsByClassName("nextPage BigButton")[0].click();
nextStep();
}
function nextStep() {
if(document.getElementsByClassName("myformTextField imageName") != undefined) {
alert('Page 2 is up.');
}
else {
alert('Page 1 is still up.');
setTimeout("nextStep()", 250);
}
}
I'm using an alert just for testing, and I keep getting the "Page 2 is up" even though it is still on page 1. I'm checking if an element, which is only present in page 2, is up. How could I make sure page2 is up?
Share Improve this question asked Jun 17, 2013 at 20:45 KingPolygonKingPolygon 4,7558 gold badges46 silver badges77 bronze badges4 Answers
Reset to default 6The major issue you're going to run into is that JavaScript variables--including functions--don't persist from one page to another. Put another way, you can't write a function on one page and have it execute on the page that replaces it.
You could pass a URL variable or set a cookie for data persistence--or store settings on your server--but a straight JavaScript approach won't work.
Of course, there is a little trick that some folks use to load the entire DOM of the next page into a variable (using a variation on an XMLHttpRequest), apply the stored settings to the object in memory, and then replace most of the document body with most of the new DOM, but that's probably far more plicated than you need, and it has to conform to same-domain requirements.
Well, your code here:
document.getElementsByClassName("myformTextField imageName")
returns an empty array when it finds nothing, which is not undefined
. Therefor, your first if condition will always be true, regardless if it finds your item or not.
Instead, check the length returned by getElementsByClassName
or use querySelector
and check for null
if (document.getElementsByClassName("myformTextField imageName").length) { ... }
// Or..
if (document.querySelector('.myformTextField.imageName') !== null) { ... }
The easiest method I chose to go with was content script matches.
What I did was create two separate javascript files in my extension: 1.js and 2.js. When awesome.page1.html loads it will run 1.js and when page2.html loads it will run 2.js.
All I did in my manifest.json file is the following:
"content_scripts":[
{"matches": ["http://awesome.page1./*"], "js": ["1.js"]},
{"matches": ["http://page2./*"], "js": ["2.js"]}]
you should try to user jquery and the $(document).ready(handler)
this is really a best practice to ensure that the page is loaded before it tries to execute your mands http://api.jquery./ready/
本文标签: htmlJavascript Performing action once the next page has loadedStack Overflow
版权声明:本文标题:html - Javascript: Performing action once the next page has loaded? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135870a2422365.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论