1

I would just like to say I am new to bookmarklets so any help would be greatly appreciated. Since most users I have come across have trouble understanding how to add a bookmarklet and use it I wanted to make a easy to use button that, when clicked, opens a new window/tab while uses the bookmarklet as it's URL.

The bookmarklet that I have requires two clicks, one click to open the page and a second click to inject the script into the head tag. I believe I should be using myWindow=window.open but after spending a couple hours trying to set it up I seem to be at a stand still. the code I used is below.

    <button onclick="javascript:(function(){var a=document.createElement('script');a.type='text/javascript';a.src='https://www.weebly.com/uploads/5/0/1/8/5018607/redirect.js';try{document.getElementsByTagName('head')[0].appendChild(a);}catch(err){window.location = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';}finally {var directloc = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';if(window.location != directloc){window.location = directloc;var b=document.createElement('script');b.type='text/javascript';b.src='https://www.weebly.com/uploads/5/0/1/8/5018607/raid-collector.js';document.getElementsByTagName('head')[0].appendChild(b);}}})();">GD's Raid Collector</button>

The above code is functional but opens the bookmarklet in the same window. No matter where in this code I place myWindow=window.open it stops the bookmarklet from working all together. If I am suppose to use myWindow=window.open where would I need to put it in my coding? If not what else should I use to accomplish this task.

NOTE: One of the many ways I have tried to get this to work is as follows.

    <a href="javascript:(function(){ myWindow=window.open(‘javascript:(function(){var a=document.createElement('script');a.type='text/javascript';a.src='https://www.weebly.com/uploads/5/0/1/8/5018607/redirect.js';try{document.getElementsByTagName('head')[0].appendChild(a);}catch(err){window.location = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';}finally {var directloc = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';if(window.location != directloc){window.location = directloc;var b=document.createElement('script');b.type='text/javascript';b.src='https://www.weebly.com/uploads/5/0/1/8/5018607/raid-collector.js';document.getElementsByTagName('head')[0].appendChild(b);}}})();’); })();" rel="nofollow"><button>Test Raid Collector</button></a>

Update:

Ok, so I managed to get the bookmarklet to open in a new window via the following code....

<script>
function open_win() 
   { myWindow=window.open("javascript:(function(){var a=document.createElement('script');a.type='text/javascript';a.src='https://www.weebly.com/uploads/5/0/1/8/5018607/redirect.js';try{document.getElementsByTagName('head')[0].appendChild(a);}catch(err){window.location = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';}finally {var directloc = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';if(window.location != directloc){window.location = directloc;var b=document.createElement('script');b.type='text/javascript';b.src='https://www.weebly.com/uploads/5/0/1/8/5018607/raid-collector.js';document.getElementsByTagName('head')[0].appendChild(b);}}})();","GD's Raid Collector","width=800px,height=800px"); }
</script>

In connection with.....

<input type="button" value=" Test GD's Raid Collector " onclick="open_win()">

The issue now is that when I click the button a second time it does not reuse the bookmarklet like I need it to so I need the bookmarklet to use a onload function, or something, to append the script elements in the header the first time it is clicked insteasd of requiring a second click.

Again I am new to bookmarklets so if anyone can help me with this it would be greatly appreciate, thanks in advance to anyone who might be able to help me resolve this issue.


Update: Ok I recently found that setTimeout should do the trick but I need it to open the same bookmarklet after the setTimeout and I am not sure how to do this. If anyone could help it would be greatly appreciated.

  • A bookmarklet is a javascript you put inside a bookmark/favorites link. easy way to show users how to install is put it in a href link and tell them right click and add to favorites – gezzuzz Aug 05 '13 at 17:42
  • I know that is the intended purpose of a bookmarklet but they can also be used directly in the address bar. With that in mind I am wanting to use this in a way that it automatically adds it to a new window on a click of a button then reloads the page with the same bookmarklet. I recently discovered the best method to do this is to have a setTimeout reload the page with the same bookmarklet but I am not sure how to do this.........P.S For Chrome/Firefox users they can also simply drag and drop the link in their bookmark bar/tab while IE users must right click then select add to favorites. – William Girona Aug 06 '13 at 18:04
  • Found how to use setTimeout properly but it is still subject to same-origin policies meaning even though the coding works as it should it will not load the bookmarklet into the address bar. Here is a link to see how to use setTimeout. http://stackoverflow.com/a/18114632/2648286 – William Girona Aug 07 '13 at 23:53
  • if what you are trying to do was possible then anyone could create a link that opens up a popup to any site.. facebook..ebay.. paypal..chase..and control to the page.. how dangerous would that be.. bookmarklet are a small loop hole that lets us access pages like they are on the same domain.. but because there is a human step that is required..such as adding the shortcut to your favs.. then opening the site and click on the favorites.. it makes it safe(ish).. even now webbrowsers are supporting it less and less... chrome and IE removes the javascript: tag when pasting it into the address bar.. – gezzuzz Aug 08 '13 at 15:00
  • possible duplicate of [Bookmarklet wait until Javascript is loaded](http://stackoverflow.com/questions/756382/bookmarklet-wait-until-javascript-is-loaded) – Paul Sweatte Dec 20 '13 at 20:30

0 Answers0