2011-07-21 62 views
0

第一H1标题和开放\\ \ h1.html我得到两个选项卡下面的代码 - 一个表示[对象]窗口和其它显示我想要的页面。 获取在新标签

  1. 什么会摆脱第一个无用的选项卡?

  2. 有没有办法让书签打开http:/// getting_started_txt_(random_alphanumeric_code_here).html ?.

...我需要打开刚与文件名开头的脱机文件名,然后一些乱码的H1部分匹配的页面。

在我结束的脱机文件是像“getting_started_txt_23468j5jg86458jm34858.html”。因此,小书签必须查找文件名以“h1 with underscores”开头的文件以及任何后面的文件。这可能吗?

window.open('http://en.wikipedia.org/wiki/' + document.getElementsByTagName('h1')[0].innerHTML.replace(/<[^>]+>/g, '').replace(/ /g, '_') + '_txt_');

所以如果我有第一个标题H1作为“入门”打开一个页面,该书签应该打开一个新的标签与URL http://(server_name)/getting_started_txt_(random_alphanumeric_code_here).html

需要注意的是,只有一个是哪部分getting_started_txt和文件名的其他部分相匹配可以是任何服务器上的文件。

回答

0

像这样的事情在大多数情况下工作。

window.open('\\\\server_name\\en\\' + encodeURIComponent(document.getElementsByTagName('h1')[0].innerHTML.replace(/<[^>]*>/g, '').replace(/\s/g, '_')) + '.html', 'win') 
0

小书签是这样的:

javascript:window.open('http://en.wikipedia.org/wiki/' + document.getElementsByTagName('h1')[0].innerHTML.replace(/<[^>]+>/g, '').replace(/ /g, '_')); 

还是这个(网址编码版本):

javascript:window.open%28%27http%3A//en.wikipedia.org/wiki/%27%20%252B%20document.getElementsByTagName%28%27h1%27%29%5B0%5D.innerHTML.replace%28/%3C%5B%5E%3E%5D%252B%3E/g%2C%20%27%27%29.replace%28/%20/g%2C%20%27_%27%29%29%3B 

哪些是这段JavaScript代码的简化版本:

// Find the first H1 node 
var h1 = document.getElementsByTagName('h1')[0]; 
// Extract the content of the node 
var title = h1.innerHTML; 
// Delete HTML tags in the content of the title 
title = title.replace(/<[^>]+>/g, ''); 
// Replace spaces with underscore symbols 
title = title.replace(/%s/g, '_'); 
// Open a new window (or tab) with the corresponding Wikipedia article 
window.open('http://en.wikipedia.org/wiki/' + title); 
+0

哇!两人都工作。只想到一个问题。我得到两个选项卡 - 一个表示[对象窗口],另一个显示我想要的页面。什么会摆脱第一个无用的选项卡? – Amit

+0

有没有办法像gets_started_ <这里有什么>?在这里? <这里的任何内容>都是字母数字。我需要 – Amit

+0

...我需要打开仅匹配第一部分页面 - H1和has_txt_ H1后添加后缀,然后一些乱码。我最后的脱机文件就像“getting_started_txt_23468j5jg86458jm34858.html”。因此,应该打开的URL必须在即时执行模式匹配...我不确定这是否可能。 – Amit