469,277 Members | 2,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,277 developers. It's quick & easy.

Replace function in URL, ignoring first instance of character


I have a bit of JavaScript for a Tell a Friend link. The person enters an email into a box clicks submit and their email programme opens a new mail with the URL from the referring page. My problem is that while some bits of the URL need replacing to make it work, others need to be left as they are.

The title for the pages are in the URL e.g. this page has the title "IDIOTS OR WHAT?!"
http://www...../mim_forums_homepage-user/mim_forums_thread.htm?masterID=195&forumID=281&sta rtPos=0&title=<b>IDIOTS%20OR%20WHAT?!</b>

I need a way to replace the punctuation in the titles into hex equivilents without changing the beginning of the URL. E.g. i need to change the ?! at the end into %3F%21 without changing the ? before the URL parameter.
Also, the titles are different for everypage, some titles might have ??? or !!! or ?!?! or ... etc. in them so ideally one bit of code to deal with all eventuallities. Is there a way that i can specify to just replace characters inside the &title= bit?

Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript' language='JavaScript'>
  2. function jsMailThisUrl()
  3. {
  4. var email_subject = 'Check out this topic';
  5. var email_body = 'I found this topic and thought you might be interested: ';
  6. //var page_url = window.self.location.href.split( '?' )[0];
  7. var page_url=window.location.href;
  8. //page_url = page_url + '';
  9. var page_url = page_url.replace("<b>", "%3Cb%3E");
  10. var page_url = page_url.replace("</b>", "%3C/b%3E");
  11. var page_url = page_url.replace(/ /g, "%20");
  12. var field = document.email_friend_script.address;
  13. window.location = 'mailto:' + field.value + '?subject=' +
  14. escape( email_subject ) + '&body=' +
  15. escape( email_body + page_url + '\n\n');
  16. }
  17. </script> 
Thanks for your help!!!
Adele x
Mar 27 '08 #1
2 1719
16,027 Expert Mod 8TB
Try using encodeURIComponent().
Mar 27 '08 #2
Try using encodeURIComponent().
Hi, thanks for this suggestion, Ill look into it (Google it) today :)
Mar 31 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Jane Doe | last post: by
5 posts views Thread by Michael Hiegemann | last post: by
5 posts views Thread by pembed2003 | last post: by
16 posts views Thread by BBM | last post: by
3 posts views Thread by Pascal | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.