此代码(如下)来自一个开源项目(SemanticScuttle) 我稍微修改了原始代码,试图将书签转换为“web useful” Javascript。
现状: Google Chrome =完美无缺!
Fiefox =功能性,但以全尺寸标签打开,而不是预先定义的尺寸。
IE = DOA =不起作用,请帮忙......
<script type="text/javascript">
var selection = '';
if (window.getSelection) {
selection = 'window.getSelection()';
} else if (document.getSelection) {
selection = 'document.getSelection()';
} else if (document.selection) {
selection = 'document.selection.createRange().text';
}
document.write('<a href="javascript:x=document;a=encodeURIComponent(x.location.href);t=encodeURIComponent(x.title);d=encodeURIComponent('+selection+');open(\'http://example.com/share/bookmarks.php/Energy?action=add&popup=1&address=\'+a+\'&title=\'+t+\'&description=\'+d,\'Site Name\',\'modal=1,status=0,scrollbars=1,toolbar=0,resizable=1,width=885,height=765,left=\'+(screen.width-885)/2+\',top=\'+(screen.height-725)/2);void 0;"><img src="images/bar/book22_5.jpg" alt="Example Code" title="Example Code" /></a>');
</script>
由于它适用于Chrome并且在Firefox中运行,我真的很想能够将它与iE一起使用。 非常感谢你。
答案 0 :(得分:2)
var selection;
if (window.getSelection) {
selection = window.getSelection();
} else if (document.getSelection) {
selection = document.getSelection();
} else if (document.selection) {
selection = document.selection.createRange().text;
} else {
selection = null;
}
if (selection !== null) {
// Do your thing i.e. take out the stuff within the document.write
// and have it execute from here.
}
答案 1 :(得分:2)
您的代码的相关部分是(重新格式化和缩写),如下所示:
open( urlToOpen, 'Site Name', options );
应改为:
open( urlToOpen, '_blank', options );
问题在于你如何使用window.open()函数。 IE认为您正在尝试打开名为“站点名称”的目标中的URL。如果您将其更改为“_blank”,它将在新窗口中打开。
查看有关sName参数@ MSDN的部分:http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx
答案 2 :(得分:1)
您可以编写一个函数来获取当前选择,然后在需要时调用它:
function getSelectedText() {
// get the selection function
var selection = window.getSelection || document.getSelection
|| document.selection;
if(selection) {
var range = selection.createRange;
if(range) {
selection = range();
}
// do something with selection
return selection.text || selection();
}
}
以上适用于所有浏览器(IE,FF,Chrome)
我还没有测试过这部分,但我想如果你一次编写一行代码,然后加入它,调试会更容易,这是一个例子(警告!需要测试):
var href = [
'x=document;',
'a=encodeURIComponent(x.location.href);',
't=encodeURIComponent(x.title);',
'd=encodeURIComponent(getSelectedText());', // call doSelection here.
'url="http://example.com/share/bookmarks.php/Energy";',
'opts="modal=1,status=0,scrollbars=1,toolbar=0,resizable=1,width=885,height=765,left=" + ((screen.width-885)/2) + ",top=" + ((screen.height-725)/2);',
'open(url + "?action=add&popup=1&address=" +a + "&title="+ t + "&description=" +d ,',
'"Site Name", opts);',
'void 0;'
].join("");
document.write('<a href="' + href + '"><img src="images/bar/book22_5.jpg" alt="Example Code" title="Example Code" /></a>');