JavaScript书签;单击按钮,重新加载页面,然后打开页面 [英] JavaScript Bookmarklet; click button, reload page, then open page

查看:137
本文介绍了JavaScript书签;单击按钮,重新加载页面,然后打开页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个遵循以下过程的小书签:

I am trying to create a bookmarklet that follows this process:

  1. 在任何页面上,单击书签
  2. 加载特定页面.
  3. 触发输入按钮,导致页面重新加载
  4. 重新加载一次;打开一个新页面;在这种情况下,是一个社交媒体页面.

这是我到目前为止写的:

This is what I've written so far:

if (!($ = window.jQuery)) { 
// Inject jQuery to make life easier  
    script = document.createElement( 'script' );  
    script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';  
    script.onload=SocialCredentials;  
    document.body.appendChild(script);  
}  
else {  
    SocialCredentials();  
}  

function SocialCredentials(){
    $('input:submit').trigger('click'); 
    window.open('http://www.facebook.com', '_blank');
}

按钮提交单击"使页面得以处理并清除用户的凭据,以便他们可以访问社交网站.

The button submit click causes the page to process and clear the credentials of the user so they can visit social sites.

上面的代码有效,但是我想确保页面在打开新的社交媒体页面之前完成加载.

The above code works, but I want to make sure the page finishes loading before opening a new social media page.

我需要添加某种wait()函数吗?如果我确实添加了一个wait方法,它将杀死JavaScript并且不打开新窗口吗?我对JavaScript相当陌生,所以我对这些类型的机制不熟悉.

Do I need to add some kind of wait() function? If I do add a wait method, will it kill the JavaScript and not open the new window? I'm fairly new to JavaScript, so I'm not familiar with these types of mechanics.

我应该注意,我无权访问或控制此书签将在其上运行的页面,因此我无法更改其流程.我正在尝试做的是更多地希望另一个部门能够作为快速解决方案,直到他们可以做出自己的更改为止.

I should note that I don't have access or control to the page this bookmarklet will work on so I can't alter it's flow. What I'm trying to do is more of a favor for another department as a quick fix until they can make changes on their end.

Edit2:更新了过程.

Updated the process.

推荐答案

如果重新加载页面,则该页面上当前正在运行的任何代码(包括小书签中的代码)都将被终止并删除.传统上,书签加载代码在页面加载后会停止工作,用户再次单击它即可.

If a page reloads, any code currently running on that page, including code from a bookmarklet, is ended and removed. Traditionally bookmarklet code ceases to work after a page load and user clicks it again.

我知道三种解决方法.

A.)更改加载页面的过程,改为使用AJAX.

A.) Change the process that loads the page to instead use AJAX.

B.)更改加载页面的过程,改为打开一个新窗口,然后使用JavaScript操纵新窗口.

B.) Change the process that loads the page to instead open a new window, and then use JavaScript to manipulate the new window.

C.)在触发页面加载之前,打开一个新的子窗口并将代码插入其中.然后,即使重新加载了父级,该子级窗口中的代码也可以监视其父级,并对父级执行操作.

C.) Before triggering the page load, open a new child window and insert code into it. The code in that child window can then monitor its parent and take actions on the parent even after the parent has reloaded.

这篇关于JavaScript书签;单击按钮,重新加载页面,然后打开页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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