www.webdeveloper.com
Results 1 to 6 of 6

Thread: Weird Javascript Encoding Issue

  1. #1
    Join Date
    Nov 2009
    Posts
    3

    Weird Javascript Encoding Issue

    Hello,

    I have a function that inserts some text into a textarea. The text is a placeholder for some data merging, and looks like %%FieldName%%. I have run into an issue where when i insert the text %%FaxNumber%%, it inserts it as '%x Number%%'.

    I've done a few tests which rules out the function, but seems the issue is with the parameter to the function. If i pass '%Fa' it converts it to . Is there anyway i can stop this from happening??

    To test this, in a blank html document add the line:
    <a href="javascript:alert('%Fa');">test</a>

    You will be prompted with ''. There is no doctype, im using quirks mode (to take advantage of 100% height).

    Thanks in advance for any help.

    Cheers,
    James

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by jimloko View Post
    There is no doctype, im using quirks mode (to take advantage of 100% height)
    You should not work in quirks mode, to take advantage of the strict standard coding. It is well known that browsers act unforeseeable in quirks mode, this is the reason for a DTD is required.

    Have you set also the meta charset?

  3. #3
    Join Date
    Aug 2009
    Posts
    593
    Just remember that &#37;FA translates to the decimal character 250 when it represents it.

    You could try escaping the string first so it appears normal:

    HTML Code:
    <a href="javascript:alert(escape('%Fa'));">test</a>

  4. #4
    Join Date
    Nov 2009
    Posts
    3
    Kor:
    Meta charset is UTF-8. This is an older app i've taken over and will be putting into a new template, with a DTD!

    thraddash:
    The only problem with escaping the string is it ends up being URL encoded, so the example field name ends up as '%25%FaxNumber%25%25', which doesn't quite do it.

    So i really dont understand why its encoding, even with a DTD and encoding specified!

    cheers

  5. #5
    Join Date
    Aug 2009
    Posts
    593
    Ok, here are some tests I have tried:

    Code:
    <b>Plain HTML</b><br />
    <input type="text" value="&#37;%FieldName%% - %%FaxNumber%%" onclick="alert(this.value);" />
    <div onclick="alert(this.innerHTML);">%%FieldName%% - %%FaxNumber%%</div>
    <textarea onclick="alert(this.value);">%%FieldName%% - %%FaxNumber%%</textarea>
    
    <hr />
    
    <b>Updated via Javascript</b><br />
    <input id="inputs" type="text" onclick="alert(this.value);" />
    <div id="divs" onclick="alert(this.innerHTML);"></div>
    <textarea id="textareas" onclick="alert(this.value);"></textarea>
    
    <hr />
    
    <a href="#" onclick="alert('%FA');">Click Me (onclick)</a><br />
    <a href="javascript:alert('%FA');">Click Me (href)</a><br />
    
    <script type="text/javascript">
    	document.getElementById("inputs").value    = "%%FieldName%% - %%FaxNumber%%";
    	document.getElementById("divs").innerHTML  = "%%FieldName%% - %%FaxNumber%%";
    	document.getElementById("textareas").value = "%%FieldName%% - %%FaxNumber%%";
    	alert("%%FieldName%% - %%FaxNumber%%");
    </script>
    Is seems using the javascript: directive is the only one causing issue here, otherwise is all looks correct to me?

  6. #6
    Join Date
    Nov 2009
    Posts
    3
    Yep, that all looks right.

    I could modify it to use the onclick event instead of javascript: on the a tag (with href="#"), but i've had some issues with that in the past where some browsers execute the onclick but nav to blank page. Returning false (like on a submit button to cancel a form post) doesn't always work.

    I'd just like to know why its encoding it. I suppose it is in the href attribute, and the javascript: just tells the browser to run the script as the URI instead of http reference. I suppose it is a URL and thus URL encoding!?!

    hmmm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles