[英]Bypassing The pop-up blocker for window.open() in javascript
I have a text file with some lines containing some key words. 我有一个文本文件,其中某些行包含一些关键字。 My exercise is to extract info from that file and create a html document inserting the liens in the text file with appropriate tags. 我的工作是从该文件中提取信息,并创建一个HTML文档,在其中将留置权插入带有适当标记的文本文件中。 for example: This is the text file: 例如:这是文本文件:
This should be in a html tag with START as class name
THIS SHOULD BE IN A HTML TAG WITH CAPITALS AS CLASS NAME
This should be in a HTML tag with CODE as class name
Now writing a javascript program to insert those lines into HTML is very easy. 现在编写一个javascript程序以将这些行插入HTML非常简单。 I just used some string handling like this: 我只是使用了一些像这样的字符串处理:
if(contents[i].indexOf(" CODE")!=-1){
w.document.write("<p class='code'>"+lines+"</p>");
}
I am writing all these into a new window.open object. 我将所有这些都写到一个新的window.open对象中。 The main problem is that pop-up blockers do not allow this functionality. 主要问题是弹出窗口阻止程序不允许此功能。 So, is there any other way I can do this? 那么,还有其他方法可以做到这一点吗? I can't generate the html file physically, I need to generate it on the fly, so window.open is the only method I could think of. 我无法物理生成html文件,我需要即时生成它,因此window.open是我能想到的唯一方法。 Are there any other ways I can accompolish this? 我还有其他方法可以做到这一点吗? (I can bypass the pop up blocker by just using (我可以仅使用跳过弹出窗口阻止程序
w=window.open("somefile.html")
w.document.opne("somefile.html")
where somefile.html is any file. 其中somefile.html是任何文件。 But I do not want to do it, it hardly seems a clean way.) 但我不想这样做,这似乎很难做到。
More over, For me to access the file, I have to host it on a server (I am currently using http-server offered by node for this) Is there any other alternative to this? 而且,对于我来说,要访问该文件,我必须将其托管在服务器上(我目前正在使用节点提供的http服务器),还有其他替代方法吗?
I do not want to use Jquery, I wish to accomplish all of this with vanilla javascript. 我不想使用Jquery,我希望使用香草javascript完成所有这些工作。 But if there is a possibility of doing this with Jquery, please let me know. 但是,如果有可能使用Jquery进行此操作,请告诉我。
Thank you very much :) 非常感谢你 :)
On principle, pop-up windows are blocked by all modern browsers. 原则上,所有现代浏览器均会阻止弹出窗口。 They'll ask you if you want to allow them, but otherwise they'll not allow them. 他们会问您是否要允许他们,否则他们将不允许您。
If it has to be another document, perhaps an iframe could be useful? 如果必须是另一个文档,也许使用iframe可能有用吗?
Here's a bunch of extra solutions: 这里有很多额外的解决方案:
<a>
tag. 基本上,这只是使用<a>
标记。 You can put the content you wish to have in the fragment for the link, and the page you open can decode the fragment and show the info you want. 您可以将所需的内容放入链接的片段中,打开的页面可以解码该片段并显示所需的信息。 Might not work with huge content. 可能无法处理大量内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.