最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass a path as a param to a JS function - Stack Overflow

programmeradmin0浏览0评论

This code is a simple function call:

<input type="submit" name="_eventId_load" onclick="toLoad('2012\9\27\15\2012-09-27T15-05-59-512.00638.eml');" value="Load"/>

This is what I receive in the js function:

 function toLoad(path) {
    document.getElementById("emailPath").value = escapepath);
}

Via firebug, I see that the param path contains: 201292-09-27T15-05-59-512.00638.eml
And after the scape function: 20129%17%0D%812-09-27T15-05-59-512.00638.eml

The values are pletely different. How could I work/edit te \ before I send it to the js function?

Thanks in advance.

EDIT

This is the solution:

<input id="filePath-${status.index}" type="hidden" value="${file.path}"/>
<input type="submit" onclick="toLoad('${status.index}'" value="Load"/>

These inputs are inside a form, each element is a file. I need ${status.index} to know which file I want to load. ${file.path} contains the path I want to replace.

function toLoad(index) {
    var emailPath = document.getElementById("filePath-" + index).value.replace(/\\/g,"\\\\");
    document.getElementById("emailPath").value = emailPath;
}

The correct input will be included in a div, so I can use it for the final functionallity.

Thanks for your answers!

This code is a simple function call:

<input type="submit" name="_eventId_load" onclick="toLoad('2012\9\27\15\2012-09-27T15-05-59-512.00638.eml');" value="Load"/>

This is what I receive in the js function:

 function toLoad(path) {
    document.getElementById("emailPath").value = escapepath);
}

Via firebug, I see that the param path contains: 201292-09-27T15-05-59-512.00638.eml
And after the scape function: 20129%17%0D%812-09-27T15-05-59-512.00638.eml

The values are pletely different. How could I work/edit te \ before I send it to the js function?

Thanks in advance.

EDIT

This is the solution:

<input id="filePath-${status.index}" type="hidden" value="${file.path}"/>
<input type="submit" onclick="toLoad('${status.index}'" value="Load"/>

These inputs are inside a form, each element is a file. I need ${status.index} to know which file I want to load. ${file.path} contains the path I want to replace.

function toLoad(index) {
    var emailPath = document.getElementById("filePath-" + index).value.replace(/\\/g,"\\\\");
    document.getElementById("emailPath").value = emailPath;
}

The correct input will be included in a div, so I can use it for the final functionallity.

Thanks for your answers!

Share Improve this question edited Sep 28, 2012 at 9:19 Blanca Hdez asked Sep 28, 2012 at 7:59 Blanca HdezBlanca Hdez 3,56319 gold badges72 silver badges95 bronze badges 2
  • toLoad != setAttachmentToLoad – Barmar Commented Sep 28, 2012 at 8:15
  • That was a typing error when I wrote the question. In my code it has the same name – Blanca Hdez Commented Sep 28, 2012 at 8:18
Add a ment  | 

3 Answers 3

Reset to default 3

The \ is an escape character and therefore needs to be duplicated to bee a literal \, that is, substitute all \ symbols with \\ and retry.

See Javascript - Replacing the escape character in a string literal

\ is reserved as an escape character in Javascript for special characters.

http://www.javascriptkit./jsref/escapesequence.shtml

Escaping \ itself happens with two \\

Use escape and unescape functions The unescape() function decodes an encoded string. http://www.w3schools./jsref/jsref_unescape.asp

发布评论

评论列表(0)

  1. 暂无评论