Javascript bookmarklet to get current url

How to get the current url with javascript code that can be used in the bookmarket? This code does not work:

javascript:copy(window.location.href);

He must copy it to the clipboard. I need to support Firefox, Chrome and IE

+3
source share
3 answers

How about a dialog from which you can copy the current URL?

javascript:void(prompt("URL:", location.href))

Part voiddoes not allow the browser to move when you click "OK" or "Cancel".

Putting a URL into the clipboard requires more work and differs from different browsers. If you really want to put data on the clipboard, specify the browsers that you need to support.

+2
source

URL- (Opera, Chrome) :

javascript:(function(s){try{s=document.selection.createRange().text}catch(_){s=document.getSelection()}prompt('','\n'+location+'\n'+s)})()

:

javascript:(function(s){try{s=document.selection.createRange().text}catch(_){s=document.getSelection()}prompt('',document.title+" "+'\n'+location+'\n'+s)})()
+3

There is no built-in function in JS called copy. If there is one on the page, then it should work. Thus, the page will be needed for this code.

How to copy to clipboard in javascript?

0
source

All Articles