0

I have an order submitting form where I want to replace URL texts with corresponding HTML links. I've found the following code on Stackoverflow:

get_url = function() {
  var urls = document.getElementById('w_descr').firstChild;
  urls.nodeValue = replaceURLWithHTMLLinks(urls.nodeValue);
}

function replaceURLWithHTMLLinks(text) {
  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  return text.replace(exp,"<a href='$1'>$1</a>"); 
}

I'm calling the get_url() function on click even of the form submitting button. It works fine. However the submitted orders have a feature of editing. If you edit an order and click on the Submit button again, the function will work again and will duplicate the existing link.

Could anybody help me to figure out how can I prevent that to happen? I mean - how to modify the script above to not to duplicate the links which are already in HTML form.

Thanks in advance.

1 Answer 1

1

Always store the text 'plain' (without the links), and only add the links when outputting the text for display.

When outputting the text for editing, output the 'plain' text.

Sign up to request clarification or add additional context in comments.

2 Comments

There's currently no way to do that. The data is stored in the database and if I call the function on all the text on document.load, it will do the work - replace the text with HTML links but the links will still look as HTML code, not actually a link. Isn't there a way to check if a link is surrounded by <a></a> and not call that function?
My answer is that you should store the data in the database as plain text (without the links). It is not inherently impossible to add the links on document.load: arnout.engelen.eu/files/dev/autolink.html . You could of course also do it server-side - that seems neater to me.

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.