如何获取

发布于 2024-10-04 15:55:55 字数 727 浏览 5 评论 0原文

我正在尝试使用 Javascript 获取 标记的内容。我成功地在 FF、Chrome、Opera 甚至 IE6 中获得了它,但在 IE7 上失败了(还没有尝试过 IE8+)。

基本上,这是简化的代码版本:

<noscript>Lorem ipsum</noscript>
<script>
    var noscript = document.getElementsByTagName('noscript')[0];
    noscript.textContent; // undefined
    noscript.innerHTML; // empty string
    noscript.childNodes.length; // 0
</script>

我尝试在内部添加元素并针对它们,但没有成功。我尝试包装父元素并获取其 .innerHTML,但 标记之间的任何内容都会被丢弃。

注意:我正在构建一个惰性加载器脚本,并且 元素正是我所需要的( src 属性内浏览器不会获取 标记。)

I'm trying to get the content of a <noscript> tag using Javascript. I succesfully managed to get it in FF, Chrome, Opera and even IE6 but fail on IE7 (haven't tried IE8+ yet).

Basically, here's the reduced code version :

<noscript>Lorem ipsum</noscript>
<script>
    var noscript = document.getElementsByTagName('noscript')[0];
    noscript.textContent; // undefined
    noscript.innerHTML; // empty string
    noscript.childNodes.length; // 0
</script>

I tried adding element inside and targeting them, no success. I tried to wrap in a parent element and getting its .innerHTML, but anything between <noscript> tags is discarded.

Note : I'm building a lazyloader script and the <noscript> element is just what I need (<img> src attributes inside a <noscript> tag are not fetched by the browser.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

夜深人未静 2024-10-11 15:55:57

您正在查询 script 元素而不是 noscript 元素,
这适用于所有浏览器:

<noscript>Lorem ipsum</noscript>
<script>
    var noscript = document.getElementsByTagName('noscript')[0];
    alert(noscript.innerHTML); // Lorem ipsum
</script>

You're querying the script elements instead of the noscript elements,
This works in all browsers:

<noscript>Lorem ipsum</noscript>
<script>
    var noscript = document.getElementsByTagName('noscript')[0];
    alert(noscript.innerHTML); // Lorem ipsum
</script>
影子的影子 2024-10-11 15:55:57

这是一个非常肮脏的黑客,完全未经测试,但我只是有这个想法:D

你可以按照其他人的建议,将内容放在 div 而不是 noscript 中,除了隐藏它之外,立即删除 所有 img 标签的 src 属性使用一些效果:

jQuery(function () { 
    jQuery('div#noscript img').attr('src', '');
  });

这应该限制(但不是消除)图像的获取。

这只是基于其他人的评论和建议的想法,希望对您有所帮助。


编辑 - 我刚刚想到的另一个肮脏的黑客,它假设您可以更改 html 所在的位置,因此可能相关也可能不相关:

在 noscript 内部有一个

<iframe width="100%" height="100%" src="?noscript=true" />

then you can XMLHttpRequest for the noscript内容...

This is a really dirty hack, and completely untested but i just had the idea :D

You could do as others have suggested, put the content in a div rather than a noscript, and in addition to hiding it, immediately remove the src attribute of all the img tags using something to the effect of:

jQuery(function () { 
    jQuery('div#noscript img').attr('src', '');
  });

This should limit (but not eliminate) the fetching of images.

This is just a though based on comments and the suggestions of others, hope it helps.


Edit - another dirty hack i've just thought of, which assumes that you can change where the html lives, so may or may not be relevant:

Inside the noscript have a

<iframe width="100%" height="100%" src="?noscript=true" />

then you can XMLHttpRequest for the noscript content...

梦中楼上月下 2024-10-11 15:55:56

在 IE 7 和 8 中,根本不可能检索 元素的内容。 HTML 中 标记之间的任何内容都不会反映在 IE 中的 DOM 中,该元素没有子元素且 innerHTMLinnerText 是空字符串。

在 IE 6 中,情况很奇怪:与 IE 7 一样, 元素没有子节点,但其内容反映在 innerHTML中。元素的 >outerHTML(但不是 innerText)属性。

在这种情况下,您在 IE 中的唯一选择是将 元素中的内容放入其他元素中。要模拟 元素的行为,您可以将内容放入由 JavaScript 立即隐藏的元素中(启用脚本时):

<div id="noscript">Lorem ipsum</div>
<script type="text/javascript">
    document.getElementById("noscript").style.display = "none";
</script>

In IE 7 and 8, it's simply impossible to retrieve the contents of a <noscript> element. Any content between the <noscript> and </noscript> tags in the HTML is not reflected in the DOM in IE, the element has no children and innerHTML and innerText are empty strings.

In IE 6, the situation is curious: in common with IE 7, the <noscript> element has no child nodes but its contents are reflected in the innerHTML and outerHTML (but not innerText) properties of the element.

All this being the case, your only option in IE is to put the content in your <noscript> element inside some other element instead. To emulate the behaviour of a <noscript> element, you could put the content in an element that is immediately hidden by JavaScript (when script is enabled):

<div id="noscript">Lorem ipsum</div>
<script type="text/javascript">
    document.getElementById("noscript").style.display = "none";
</script>
下雨或天晴 2024-10-11 15:55:56

这不是最优雅的解决方案 - 图像在 IE7 和 IE8 中正常加载,而所有其他浏览器都获得了延迟加载的额外好处。最终输出中会出现一些空的注释块……但谁在乎呢,对吧?

<!--[if (gt IE 8)|!(IE)]><!--><noscript><!--<![endif]-->
  <img src="/path/to/img.jpg" alt="photo" />
<!--[if (gt IE 8)|!(IE)]><!--></noscript><!--<![endif]-->

JavaScript(jQuery - 如果您需要真正的 JS,请告诉我):

jQuery(function($) {
  $('noscript').each(function() {
    var $this = $(this);
    $this.replaceWith($this.text());
  });
});

使用 IE7+、Chrome、FF3.6+、Opera11 进行测试

Not the most elegant solution - Images load as normal with IE7 and IE8, and all other browsers get the added benefit of lazi loading. You will end up with some empty comment blocks in the final output... but who cares, right?

<!--[if (gt IE 8)|!(IE)]><!--><noscript><!--<![endif]-->
  <img src="/path/to/img.jpg" alt="photo" />
<!--[if (gt IE 8)|!(IE)]><!--></noscript><!--<![endif]-->

JavaScript (jQuery - if you need real JS let me know):

jQuery(function($) {
  $('noscript').each(function() {
    var $this = $(this);
    $this.replaceWith($this.text());
  });
});

Tested with IE7+, Chrome, FF3.6+, Opera11

迷乱花海 2024-10-11 15:55:56

解决此问题的一种解决方法是将 noscript 的内容复制为其属性。

例如:

<noscript id="ns" alt="Lorem ipsulum">Lurem ipsulum</noscript>

在脚本中获取 alt 属性的值而不是其innerHTML

<script>
   var ns = document.getElementByid('ns');
   var htm = ns.innerHTML || ns.getAttribute('alt');

   alert(htm);

</script> 

One work around for this is to duplicate the content of noscript as its attribute.

For example:

<noscript id="ns" alt="Lorem ipsulum">Lurem ipsulum</noscript>

On the script get the value of alt attribute instead of its innerHTML

<script>
   var ns = document.getElementByid('ns');
   var htm = ns.innerHTML || ns.getAttribute('alt');

   alert(htm);

</script> 
千紇 2024-10-11 15:55:56

您可以在大多数现代浏览器中访问 noscript 标签 textContent 属性。对于较旧的浏览器,我编写了一个运行速度相当快的填充程序,并且在我可以测试的所有桌面和移动浏览器中。

查看博文:
http://queryj. wordpress.com/2012/07/06/read-noscript-tag-content-reliously-in-all-browsers/

查看代码(根据 MIT 和 GPL 许可证双重许可):
https://github.com/jameswestgate/noscript-textcontent/

编辑:

该脚本的工作原理是通过 ajax 请求重新加载页面(应缓存在浏览器中),并为不支持 textContent 属性的浏览器手动解析内容

You can access the noscript tag textContent property in most modern browsers. For older browsers I have written a polyfill which runs pretty quickly and in all desktop and mobile browsers I could test.

View the blog post:
http://queryj.wordpress.com/2012/07/06/read-noscript-tag-content-reliably-in-all-browsers/

View the code (Dual licensed under the MIT and GPL licenses):
https://github.com/jameswestgate/noscript-textcontent/

EDIT:

The script works by reloading the page through an ajax request (which should be cached in the browser) and parsing the contents manually for browsers which do not support the textContent property

那一片橙海, 2024-10-11 15:55:56

您可以

<script>document.write('<script id="images" type="text/html">')</script>

在图像之前然后

<script>document.write('</scr'+'ipt>')</script>

在图像之后。您无需使用 JavaScript 即可获得图像,而使用 JavaScript 则可以在 $('images').innerHTML 中使用它们的源代码

You could

<script>document.write('<script id="images" type="text/html">')</script>

just before the images and then

<script>document.write('</scr'+'ipt>')</script>

after it. You'd have the images available without javascript, and with JavaScript there, you'd have their sources at your disposal in $('images').innerHTML

阳光下慵懒的猫 2024-10-11 15:55:56

我有一个不同的方法,这里有一个工作示例:
https://github.com/jokeyrhyme/noscript.js/blob /c104c480b595278e9d559aaf8a26668b7c852f72/noscript.js#L25

我的方法很简单:

  1. 执行 XHR 下载原始 HTML 的原始副本
  2. 查找 DOM 中的所有 noscript 元素
  3. 查找原始 HTML 中的所有 noscript 元素
  4. 将 DOM noscript 替换为原始 noscripts 的内容

我的 noscript.show() 方法首先测试这是否有必要。对于大多数浏览器,它只是将 noscript 元素的内容移出,以便它们可见。

被这些糟糕的浏览器困住的用户可能别无选择,并且可能习惯了缓慢的体验,因此额外的网络请求和一些正则表达式不会太糟糕。

注意:由于同源规则,当您通过 file:///... 浏览本地磁盘上的文件时,使用此技术可能会遇到问题,因此它可能仅在通过以下方式浏览时有效http://...https://...

I have a different approach, with a working example of it here:
https://github.com/jokeyrhyme/noscript.js/blob/c104c480b595278e9d559aaf8a26668b7c852f72/noscript.js#L25

My approach is simple:

  1. perform an XHR to download a raw copy of the original HTML
  2. find all noscript elements in the DOM
  3. find all noscript elements in the original HTML
  4. replace the DOM noscripts with the contents of the original noscripts

My noscript.show() method tests whether this is necessary first. For most browsers, it just moves the contents of the noscript elements out so that they are visible.

Users who are stuck with these terrible browsers probably have no choice about it, and are probably used to having a slow experience, so an extra network request and some regular expressions aren't going to be too bad.

Note: due to the Same-Origin rule, you may have trouble using this technique when browsing files on your local disk via file:///..., so it's likely to work only when browsing via http://... or https://....

养猫人 2024-10-11 15:55:56

我目前没有 ie 浏览器;) 但据我所知,当您获取其父标签的 .innerHTML 时, 标签的内容是可用的()。然后由你来将其取出(例如,使用令人讨厌的正则表达式)

我从现在起至少 7 小时内无法测试它,所以我按原样发布此信息。

附言。我的建议是重新设计。 Noscript 标签用于包含脚本打开时不使用的内容。

I don't have an ie browser on me at the moment ;) but as far as I remember the content of a <noscript> tag is avaliable when you get its parent tag's .innerHTML(). Then it's up to you to get it out (with a nasty regexp for example)

I can't test it for at least 7 hours from now, so I post this info as is.

PS. My advice is to redesign. Noscript tags are ment to contain stuff that is NOT used when scripts are on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文