aslum
aslum

Reputation: 12264

My javascript bookmarklet does nothing, where did I mess up?

So I've got this bookmarklet which allows me to quickly switch to the test server version of whatever page I'm on:

javascript:(function() {window.location=window.location.toString().replace(/^http:\/\/www\./,'http://www-test.');})()

I'd also like to be able to switch to debug mode on my webpages, so I tried making a bookmarklet as below, but it doesn't seem to work:

javascript:(function() {window.location=window.location.toString().replace(/^php/,'php?action=debug');})()

What did I screw up?

Upvotes: 0

Views: 125

Answers (3)

Michael Berkowski
Michael Berkowski

Reputation: 270727

Probably it is the ^ before php. Since php occurs at the end of the string, you need to anchor it to the right with $ rather than left:

javascript:(function() {window.location=window.location.toString().replace(/php$/,'php?action=debug');})()

Upvotes: 1

Rob W
Rob W

Reputation: 349182

/^php/ only affects a string which starts with php. Since the location.href property always includes the protocol, your code doesn't do anything.

You might be looking for /php$/, which matches php at the end of the string.

To avoid an accidental refresh upon activation of the bookmarklet, you can use:

javascript:(function(){
     if(/php$/.test(location.href)) location.href += '?action=debug';
})()

Upvotes: 1

pimvdb
pimvdb

Reputation: 154928

/^php/ matches a string that starts with "php". I guess you want to match the end of a string:

/php$/

Upvotes: 1

Related Questions