0

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

  1. From any page, click the bookmarklet
  2. load a specific page.
  3. Trigger an input button which causes page to reload
  4. Once reloaded; open a new page; in this case a social media page.

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.

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.

Edit: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.

Emmanuel F
  • 1,125
  • 1
  • 15
  • 34
  • so the page posts back and fires off the new window at the same time? is that the correct understanding? – origin1tech May 22 '13 at 23:18
  • Well, I want it to post back, and then once that's done loading, fire off the new window. But, my concern is if the page does the post back, and reloads, does it lose the bookmarklet code and not open a new window? – Emmanuel F May 23 '13 at 02:31

1 Answers1

2

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.

There are three workarounds that I know of.

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

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

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.

DG.
  • 3,417
  • 2
  • 23
  • 28
  • I'm going to have to try option C. I don't have access or control to the page itself, so I can't alter it by any means; it's run by a different department, and what I'm trying to do is kind of a favor for them and my boss. Are there any resources you recommend I look up? Thank you for the help! – Emmanuel F May 23 '13 at 14:14
  • Well, that didn't work... I mean, it works if I'm at the page, however if I'm on any page, click the bookmarklet, redirect to the page I need, then process the data, it won't work since the JavaScript gets cleared out. – Emmanuel F May 23 '13 at 20:09
  • I think possibly you may not understand what I am suggesting for C. The new child window is created for only 1 purpose, to monitor it's parent so it can re-inject the bookmarklet code into the parent as necessary. This is not a very obvious and trivial thing to do. Also, the child window must be on the same domain, which should probably be possible also by creating a new document from scratch with document.write; see this: http://stackoverflow.com/a/10946937/867944. – DG. May 23 '13 at 23:47