943,891 Members | Top Members by Rank

Ad:
May 25th, 2009
0

trim a string help

Expand Post »
i have a string like this:


JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. location = '#http://www.website.com';

how can i take off that hash sine?
regards
Similar Threads
Reputation Points: 11
Solved Threads: 4
Junior Poster
sacarias40 is offline Offline
113 posts
since Nov 2008
May 25th, 2009
0

Re: trim a string help

You can use the replace() method.
e.g.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. loc = "#http://www.wesite.com/".replace(/#/ig, "");
  2. alert( loc );
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
May 25th, 2009
0

Re: trim a string help

thank you
Reputation Points: 11
Solved Threads: 4
Junior Poster
sacarias40 is offline Offline
113 posts
since Nov 2008
May 25th, 2009
0

Re: trim a string help

Click to Expand / Collapse  Quote originally posted by essential ...
You can use the replace() method.
e.g.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. loc = "#http://www.wesite.com/".replace(/#/ig, "");
  2. alert( loc );
You don't want to do a global search on this one, just remove the hash at the beginning. Otherwise, the URL you want to redirect to may have an anchor on it, e.g. #http://www.website.com/test.html#Contacts .

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. loc = "#http://www.wesite.com/".replace(/^#*/i, "");
  2. alert(loc);
Last edited by itsjareds; May 25th, 2009 at 3:30 am.
Reputation Points: 39
Solved Threads: 13
Junior Poster
itsjareds is offline Offline
103 posts
since May 2009
May 25th, 2009
0

Re: trim a string help

No it's not that bad.
If you will apply proper techniques and good workaround in your script, im sure everything can be organized.

Here's a short example, so that you can see the big difference between the codes.
javascript Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  7. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  8. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  9. <title>CROSS-PLATFORM</title>
  10. <script type="text/javascript">
  11. // <![CDATA[
  12. var include, url;
  13.  
  14. include = function( url ) {
  15. loc = "#http://www.web#site.#com/#contact".replace(/^#*/i, "");
  16.  
  17. alert( loc ); // Check the first pattern and observed the output w/o g-flag.
  18.  
  19. delete loc;
  20.  
  21. loc = "#http://www.web#site.#com/".replace(/#/ig, ""); // With g-flag's!
  22.  
  23. alert( loc + url ) // Now we will use it with a global flag and by adding up some arguments' we can organized everything and also remove unnecessary characters in the url.
  24. }
  25.  
  26. window.onload = include( "#contact" );
  27.  
  28. // ]]>
  29. </script>
  30. </head>
  31. <body>
  32.  
  33. </body>
  34. </html>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
May 25th, 2009
0

Re: trim a string help

I suppose it depends on what your circumstances are. I assumed that he was using AJAX and had a link like what he proposed. If he just needed to remove the first #, he could also have done:

JavaScript Syntax (Toggle Plain Text)
  1. var string = "#http://www.example.com";
  2. var newString = string.substr(1);
Reputation Points: 39
Solved Threads: 13
Junior Poster
itsjareds is offline Offline
103 posts
since May 2009
May 25th, 2009
0

Re: trim a string help

Hi itsjared,

It's was just a logical example of using g-flag's.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: How to display the details in right frame?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript/flash onClick Issues





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC