Calling content via Ajax, JavaScript in this page disappears on load

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BobC
    New Member
    • Mar 2012
    • 2

    Calling content via Ajax, JavaScript in this page disappears on load

    I am using a piece of inline Javascript to cloak my e-mail address on a webpage, to prevent spam. The HTML text fragment including the script ( xxx.txt ) is loaded dynamically using an AJAX call and inserted into a DIV.

    However, when it is displayed the javascript has disappeared: instead of a clickable link similar to a simple mailto: link there is nothing.

    What makes the javascript snippet disappear and how can I prevent that ?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Could you post your code, so that we can take a look.

    Comment

    • BobC
      New Member
      • Mar 2012
      • 2

      #3
      I'm calling the dynamic content as a .txt file and insert it in a div, with a command like this:

      Code:
      liveData03.update('doc=docs/filename.txt');
      The txt file contains standard html and javascript; the cloaked email address. It looks like this:

      Code:
      <p>[I] some text  [/I]<script type='text/javascript'>var v2="DNZHGHBJ4J56WG43YI";var v7=unescape("+%3C%3E-5%3B%02%25F%23CS3%26%1AP6%24");var v5=v2.length;var v1="";for(var v4=0;v4<v5;v4++){v1+=String.fromCharCode(v2.charCodeAt(v4)^v7.charCodeAt(v4));}document.write('<a href="javascript:void(0)" onclick="window.location=\'mail\u0074o\u003a'+v1+'?subject=Where%20is%20my%20order?'+'\'">'+'contact us<\/a>');</script>[I] more text.[/I]</p>
      When shown on the screen the javascript does not show the email address it is supposed to show; in other words it is simply not executed, I guess.

      All help is appreciated!!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The JavaScript is just text at moment. It needs to be evaluated/executed.

        You can either

        1. dynamically inject into the head (as long as your JavaScript can run from the head section which, judging from your code, it probably won't) or
        2. execute the JavaScript code using eval if the code is safe to run. You'll need to separate the JavaScript code from the HTML. Add the HTML update as normal and then execute the JavaScript.

        Another option is to use the server-side code to display the email. Yet another option is have the JavaScript code already available on the page and just use this call to add the necessary code.

        You may require a slight change to your JavaScript code.

        Comment

        Working...