2

Say I have a variable

tab_something

I need to drop the tab_ bit.

In php it's easy, using str_replace...

Will

tabSelect = document.location.hash.substr(1,document.location.hash.length);

(which would always be tab_something)

document.write(tabSelect.replace(/tab_/i, ""));

Work the way I would like it to, consistently across all modern browsers (ie6+) ?

Cheers.

4
  • 9
    Referring to IE6 as a "modern browser" might be a bit of a stretch :) Commented Dec 10, 2009 at 14:35
  • 1
    The problem is IE6 is still "modernly used", much to our dismay Commented Dec 10, 2009 at 14:37
  • I know I know, I hate ie6 myself and the word modern is highly inappropriate. Commented Dec 10, 2009 at 14:52
  • "Modern" implies after the 1500s.... Commented Mar 1, 2010 at 18:09

5 Answers 5

2

Abusing source code rewrite as a substitute for reflection is … possible. I hate to state the obvious, but: maybe take a step back and see if you can reshape the project a bit, such that you can come up with a cleaner solution?

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

2 Comments

I don't think the OP wants to do any kind of source code rewriting, but the title of the question sure makes it sound that way.
That did spring to my head, however I need both variables, as tab_something, shows up a relevant div element, while something highlights it on the nav. (and yes, it should probably be the other way round)
2

A couple of things:

  • document.location will be deprecated at some point by document.URL, consider using window.location.
  • Consider also using String.substring, since it is part of the ECMA-262 Spec.

var tabSelect = window.location.hash.substring(1); // remove "#"
tabSelect = tabSelect.replace(/tab_/i, "");        // remove "tab_"

It will work on old and modern browsers.

Comments

1

If document.location.hash always contains tab_ + some other string that you wish to retrieve, why not take advantage of the prefix always being the same length? You already have call substring() so why not let this function cut of a few more chars?

window.location.hash.substring(5)

Thanks to CMS for pointing out that window.location is preferred to document.location.

Comments

1

Yes it will. And also note that you don't have to use regular expressions in .replace(), .replace('tab_', ''); will do just fine.

1 Comment

You don't even have to use replace if you know that you always want to remove the first k chars of the string, cf. my answer.
0

Yes that is a standard JavaScript function that's been around long enough to cover all modern browsers ie6 and above. Should work just fine.

You could also use substring if you know it will always be the first 4 characters. tabSelect.substring(4) will give you everything starting the first character after tab_ (it is 0-based).

Comments

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.