Javascript函数没有被正确调用

时间:2015-11-06 06:17:58

标签: javascript

我有一个JavaScript函数

function createHyperLinkDraft(){ 
    var xWin = window.opener;
    var hyperLink = document.addHyperLinkForm.hyperLinkNameDraft.value;

    if(hyperLink){

        var urlList = "openDraftFilesAction.action?draftID="+ document.addHyperLinkForm.DraftNo.value ;
        hyperLinkName = "&nbsp;<a style='text-decoration:underline;cursor:pointer' onclick=javascript:window.open('"+urlList+"','subWindow','HEIGHT=600,WIDTH=600,screenX=100,left=100,screenY=100,top=100')>"+ hyperLink +"</a>&nbsp;";

        xWin.Xinha._currentlyActiveEditor.insertHTML(hyperLinkName);

        document.addHyperLinkForm.reset();
        window.close();
    }
}

这将存储在注释编辑器中。但是当我在我的动作类中看到它被存储为

<p>l&nbsp;<a onclick="javascript:window.open('openDraftFilesAction.action? draftID=9/1021/2015-FT-COORD-new" 3?,?subwindow?,?height="600,WIDTH=600,screenX=100,left=100,screenY=100,top=100')" style="cursor: pointer; text-decoration: underline">link</a>&nbsp;</p>

凡说实际草案ID为9/1021/2015-FT-COORD-新文件12oct / 3.

因此,该草案尚未公开。我不明白为什么会这样。

1 个答案:

答案 0 :(得分:1)

您需要使用encodeURIComponent()

对其进行编码

来自MDN:

  

encodeURIComponent()方法通过将某些字符的每个实例替换为表示UTF-8编码的一个,两个,三个或四个转义序列来编码统一资源标识符(URI)组件。字符(对于由两个&#34;代理&#34;字符组成的字符,只有四个转义序列。)

function createHyperLinkDraft() { 
    var xWin = window.opener;
    var hyperLink = document.addHyperLinkForm.hyperLinkNameDraft.value;

    if (hyperLink) {
        var urlID = document.addHyperLinkForm.DraftNo.value;
        urlID = encodeURIComponent(urlID);
        var urlList = "openDraftFilesAction.action?draftID="+ urlID;
        hyperLinkName = "&nbsp;<a style='text-decoration:underline;cursor:pointer' onclick=javascript:window.open('"+urlList+"','subWindow','HEIGHT=600,WIDTH=600,screenX=100,left=100,screenY=100,top=100')>"+ hyperLink +"</a>&nbsp;";

        xWin.Xinha._currentlyActiveEditor.insertHTML(hyperLinkName);

        document.addHyperLinkForm.reset();
        window.close();
    }
}