i have a string like this:

location = '#http://www.website.com';

how can i take off that hash sine?
regards

Recommended Answers

All 6 Replies

You can use the replace() method.
e.g.

loc = "#http://www.wesite.com/".replace(/#/ig, "");
   alert( loc );

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.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>CROSS-PLATFORM</title>
<script type="text/javascript">
// <![CDATA[
var include, url;

include = function( url ) {
   loc = "#http://www.web#site.#com/#contact".replace(/^#*/i, "");

  alert( loc ); // Check the first pattern and observed the output w/o g-flag.

  delete loc;

  loc = "#http://www.web#site.#com/".replace(/#/ig, ""); // With g-flag's!

  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. 
}

window.onload = include( "#contact" );

// ]]>
</script>
</head>
<body>

</body>
</html>

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:

var string = "#http://www.example.com";
var newString = string.substr(1);

Hi itsjared,

It's was just a logical example of using g-flag's.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.