[英]How can this JavaScript snippet be improved?
This code (below) comes from an Open-source project (SemanticScuttle) I slightly modified the original code, attempting to convert a bookmarklet, into "web usable" Javascript. 这段代码(如下)来自一个开源项目(SemanticScuttle),我对原始代码进行了一些修改,试图将书签转换为“可使用网络的” Javascript。
Current Status: Google Chrome = Works perfectly! 当前状态:谷歌浏览器=完美运行!
Fiefox = Functional, but opens in a full sized tab, instead of the pre-defined size. Fiefox =功能,但在完整尺寸的标签中打开,而不是在预定义的尺寸中打开。
IE = DOA = Not functioning, please help... 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>
Since it works in Chrome and functions in Firefox, I would really Love to be able to use it with iE also. 由于它可以在Chrome浏览器中运行并且可以在Firefox中运行,因此我非常希望能够与iE一起使用它。 Thank you very much.
非常感谢你。
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.
}
The relevant portion of your code is (reformatted & abbreviated) as follows: 您代码的相关部分(重新格式化和缩写)如下:
open( urlToOpen, 'Site Name', options );
It should read instead: 它应改为:
open( urlToOpen, '_blank', options );
The problem is with how you're using the window.open() function. 问题在于您如何使用window.open()函数。 IE thinks you're trying to open the url in a target called "Site Name".
IE认为您正在尝试在名为“站点名称”的目标中打开URL。 If you change it to "_blank" it will open in a new window.
如果将其更改为“ _blank”,它将在新窗口中打开。
Check the section about the sName parameter @ MSDN: http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx 检查有关sName参数的部分@ MSDN: http : //msdn.microsoft.com/zh-cn/library/ms536651( VS.85) .aspx
You could write a function to get the current selection as follows and then call it when you need it: 您可以编写一个函数来获取当前选择,如下所示,然后在需要时调用它:
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();
}
}
The above works in all browsers (IE, FF, Chrome) 以上内容适用于所有浏览器(IE,FF,Chrome)
I havent tested this part but i think if you write your code one line at a time and then later join it, it would be easier to debug, here's an example (Warning! needs testing): 我还没有测试这部分,但是我想如果您一次写一行代码,然后再加入它,则调试起来会更容易,这是一个示例(警告!需要测试):
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>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.