0

I have a situation where I should append html with jQuery. Within this html there is an a-tag with javascript as link.

How can I solve this?

$(".messages-wrapper").append('<li class="chat-wrapper"><a class="chat" href="javascript:openChat('http://domain.com/Messenger.aspx'">Chat öffnen<span></span><small>Chat öffnen</small></a></li>')

Thanks!!

0

4 Answers 4

4

You can escape single or double quotes in a string with a backslash.

'... href="javascript:openChat(\'http...\')">...'
Sign up to request clarification or add additional context in comments.

Comments

3

You're probably having problems because of the quotes in the string. The single quotes are terminating the string early and causing a javascript error.

Consider, however, applying the hander at the same time that you append the html.

 var link = $('<li class="chat-wrapper"><a class="chat" href="http://domain.com/Messenger.aspx">Chat öffnen<span></span><small>Chat öffnen</small></a></li>')
            .find('a')
            .click( function() {
                 openChat($(this).attr('href'));
                 return false;
            });
 link.appendTo(".messages-wrapper");

5 Comments

+1, will degrade gracefully if javascript is not enabled, though I'm guessing the chat page needs JS anyway.
@Joel: A bit moot to consider how code created with Javascript would behave if Javascript was not enabled...
You are missing the point completely. If Javascript isn't enabled, the code that creates the link doesn't execute.
@Guffa, you are correct. However, I still discourage embedding javascript in markup, even javascript generated markup.
@Guffa - you're right, of course, you caught me before my morning coffee and I responded without re-reading my answer. In my case what I usually do is simply put the HTML inline and add handlers later so I assumed that's how I had answered. If the chat page does require javascript, though, this would be a better way to add the HTML as it then wouldn't exist unless javascript were enabled.
2

You just have to escape the apostrophes in the string using \':

$(".messages-wrapper")
.append('<li class="chat-wrapper"><a class="chat" href="javascript:openChat(\'http://domain.com/Messenger.aspx\'">Chat öffnen<span></span><small>Chat öffnen</small></a></li>');

Comments

1

Just use a nested append off of a created opject, to do the click action.

$('.messages-wrapper')
  .append(
    $('<li class="chat-wrapper" />")
      .append(
        $('<a class="chat" href="http://domain.com/Messenger.aspx"><small>Chat öffnen</small></a>')
          .click(function(){
            openChat(this.href);
            return false;
          })
      )
  );

This is easier to read the intention anyway, imho.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.