简体   繁体   English

从网页中的链接“按原样”复制JavaScript URL吗?:

[英]Copy a javascript URL 'as-is' from a link in a web page?:

I want to copy a javascript URL-char for char. 我想为char复制一个JavaScript URL-char。 How, for example, would I successfully copy the javascript from the 'View Source' link on this page: 例如,如何从此页面上的“查看源代码”链接成功复制javascript:

http://javascript.about.com/library/blsource.htm http://javascript.about.com/library/blsource.htm

doing something like(?): 做类似的事情?

(function(){
    var w=open('','');
    with(w.document) { 
        write(encodeBlahComponent(document.activeElement.href).replace(/blah/g,'asii equivalent').replace(/blah/g,'unicode equivalent').replace(/blah/g,'entity equivalent'));
        close();
    }
})()

What encoding should I use and how to script it properly? 我应该使用哪种编码以及如何正确编写脚本?

If you're document.write ​ing to an HTML document, any text you output would have to be HTML-escaped: 如果你是document.write荷兰国际集团到一个HTML文件,任何文本输出,你就必须是HTML转义:

function encodeHTML(s) { // for text content and attribute values with " delimiter
    return s.split('&').join('&amp;').split('<').join('&lt;').split('"').join('&quot;');
}

somedocument.write(encodeHTML(link.href));

However it would probably be easier to use DOM methods: 但是,使用DOM方法可能会更容易:

somedocument.write('<p id="out">x</p>');
somedocument.getElementById('out').firstChild.data= link.href;

Either way you don't have to worry about Unicode or &#...; 无论哪种方式,您都不必担心Unicode或&#...; character references. 字符引用。 JavaScript strings are natively Unicode. JavaScript字符串本身就是Unicode。 And you would only need to think about using encodeURI if you are creating a URI from some script (eg. var uri= 'javascript:'+encodeURI(somejscode) ), which you're not here, you've already got a URI in the link. 而且,如果您是通过某些脚本(例如var uri= 'javascript:'+encodeURI(somejscode) )创建URI,则只需要考虑使用encodeURI ,而您不在这里,您已经有了一个URI在链接中。 ( encodeURIComponent would also work, but for this case where you have a whole URI not just a component, encodeURI will give simpler results.) encodeURIComponent也可以使用,但是在这种情况下,您拥有一个完整的URI而不仅仅是一个组件, encodeURI将给出更简单的结果。)

PS. PS。 You don't want to use the with statement ever, if you can help it. 如果可以的话,您永远都不想使用with语句。 (Or javascript: URLs, for that matter!) (或者javascript: URL,就这样!)

ETA. 预计到达时间 If you really need the original source with all errors intact, you would have to do like web-sniffer does and fetch the page again from the network. 如果您确实需要完整无缺的原始源代码,则必须像网络嗅探器一样,并再次从网络获取页面。 You might do this for the current page as long as it is the result of a GET method, using an XMLHttpRequest. 您可以对当前页面执行此操作,只要它是使用XMLHttpRequest的GET方法的结果即可。 For example: 例如:

var d= window.open().document, x= new XMLHttpRequest();
d.write('<body><pre>x</pre>');
d.close();
x.onreadystatechange= function() {
    if (this.readyState===4)
        d.body.firstChild.firstChild.data= this.responseText;
}
x.open('GET', location.href);
x.send(null);

Or, packed into a bookmarklet: 或者,打包成小书签:

javascript:(function()%7Bvar%20d=window.open().document,x=new%20XMLHttpRequest();d.write('%3Cbody%3E%3Cpre%3Ex%3C/pre%3E');d.close();x.onreadystatechange=function()%7Bif(this.readyState===4)d.body.firstChild.firstChild.data=this.responseText%7D;x.open('GET',location.href);x.send(null)%7D)()

Re: "Encoding is a function that is needed when you go from one context to another, such as from raw text to HTML source. If you take the href of a link, you've got a URL in a string. If the context you want to use that URL in is just as a URL then there is nothing more you need to do. In that case the way to “clone” a JavaScript string is to say just var newstring= oldstring. If the target context is HTML source written by document.write, you only need to HTML-encode it; nothing else.". 回复:“编码是一种功能,当您从一个上下文转到另一个上下文时,例如从原始文本到HTML源代码。如果使用链接的href,则字符串中会有一个URL。如果上下文如果您想使用该URL作为一个URL,那么您就不需要做任何其他事情了,在这种情况下,“克隆” JavaScript字符串的方法就是说var newstring = oldstring。如果目标上下文是HTML源由document.write编写,您只需要对其进行HTML编码即可;”。

Looks like I've been making a mountain out of a molehill. 看起来我一直在从小丘上爬山。 Your advice didn't sink in until now. 直到现在您的建议都没有得到体现。 It was that crud "View Source" bookmarklet that threw me (I thought the author was supposed to be a JS guru!). 正是那个粗糙的“查看源代码”书签使我感到震惊(我以为作者应该是JS专家!)。 Another one of those "learning experience" moments I suppose. 我想那是另一个“学习经历”的时刻。 Never mind. 没关系。 Many thanks for your support. 非常感谢您的支持。 Thread closed and thank you again. 线程已关闭,再次感谢您。

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

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