简体   繁体   English

Javascript Bookmarklet获取当前URL

[英]Javascript Bookmarklet to get current URL

How would I get the current URL with a javascript code that could be used in a bookmarklet? 如何使用可在小书签中使用的javascript代码获取当前URL? This code isn't working: 此代码无效:

javascript:copy(window.location.href);

It needs to copy it to the clipboard. 它需要将其复制到剪贴板。 I need to support Firefox, Chrome and IE 我需要支持Firefox,Chrome和IE

What about a dialog from which you can copy the current URL? 您可以从中复制当前URL的对话框怎么样?

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

The void part prevents the browser from navigating away when pressing OK or Cancel. 按OK或取消时, void部分会阻止浏览器导航。

Putting the URL into the clipboard takes more work, and differs on distinct browsers. 将URL放入剪贴板需要更多工作,并且在不同浏览器上有所不同。 If you really want to put the data in the clipboard, mention the browsers you need to support. 如果您确实想将数据放入剪贴板,请提及您需要支持的浏览器。

To get the URL from any legit browser (Opera, Chrome) with a bookmarklet: 要使用书签从任何合法浏览器(Opera,Chrome)获取URL:

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

If you want to add the Page Title: 如果要添加页面标题:

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

There is no built-in function in JS called copy. JS中没有内置函数称为copy。 If there is one in the page, then it should work. 如果页面中有一个,那么它应该工作。 So the page would need this code 所以页面需要这个代码

How do I copy to the clipboard in JavaScript? 如何使用JavaScript复制到剪贴板?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM