Javascript beginner: how to replace a href text if it matches a specified string? -
when posts link page on website, i'd shorten href text like: http://mywebsite.com/posts/8 /posts/8 or http://mywebsite.com/tags/8 /tags/8. since i'm learning javascript don't want depend on library prototype or jquery. recommended use javascript's replace method?
i found w3schools' page here code replacing instances of string, not href text.
here's have far:
<script type="text/javascript" charset="utf-8"> var str="http://www.mywebsite.com"; document.write(str.replace("http://www.", "")); </script>
str = str.replace(/^http:\/\/www.mywebsite.com/, ""); someelement.appendchild(document.createtextnode(str)); note you're introducing cross-site scripting vulnerability directly calling document.write user input (you you're not treating url http://<script>alert('xss');</script> correctly).
instead of using document.write, replace someelement in above code element in code should contain user content. notice code can not @ javascript top level, should instead called when load event fires.
Comments
Post a Comment