Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

whatisthe

macrumors member
Original poster
Apr 10, 2008
75
0
CT
I'm really quite new to programming and am trying to make a bookmarklet that will replace a character in a certain part of a url.


It would be something that would turn a url from this:
http://google.com/go/for/it/changethis.html
to
http://google.com/go/for/it/crangethis.html

where the h in the last part of the url is replaced with an r.


So far I have this:

javascript:
var url = new String(window.location);
var parts = url.substr(7).split('/', 5);
parts[4].replace(/h/, "r");
window.location = 'http://' + parts[0] + '/' + parts[1] + '/' + parts[2] + '/' + parts[3] + '/' + parts[4];


Everything works fine except for the " parts[4].replace(/h/, "r"); " line, which doesn't seem to do anything.


I know practically nothing about javascript so please be gentle when telling me I'm an idiot ;)

Thanks :D,
whatisthe
 
You should change the title. Javascript has absolutely nothing to do with real Java: it's just a deceptive marketing name.
 
You should change the title. Javascript has absolutely nothing to do with real Java: it's just a deceptive marketing name.

My bad, I didn't realize java isn't short for javascript :eek:
I don't think I can change the title now, but I would if I could..


Anyways, I sort of figured out a solution with the assistance of my friend but it doesn't work as well as I'd hoped.

Here's the code:

Code:
javascript:
var url = new String(window.location);
var parts = url.substr(7).split('/', 5);
var str = parts[4];
var i = parts[4].indexOf('h');
var tempStr = parts[4].substr(0,i) + 'r' + parts[4].substr(i+1,parts[4].length);
window.location = 'http://' + parts[0] + '/' + parts[1] + '/' + parts[2] + '/' + parts[3] + '/' + tempStr;


The problem is it is dependent entirely on how many split parts there are, where ideally it would always take the last part of the url to change the letter, no matter how many parts of the url there are.

Anyone care to give any insight :)
 
Arrays are objects in javascript, and they have a member called "length" that you can use to achieve this. Something like:

Code:
var parts = window.location.substr(7).split('/', 5);
parts[parts.length-1] = parts[parts.length-1].replace("h","r");
window.location = "http://" + parts.join("/");

Assuming a URL starts with http:// is probably bad form, but i'll leave it as an exercise to you to figure out how to deal with, say, https:// instead.

Also, there is a web programming forum where this may be more applicable. If you weren't using DOM elements and just wanting to learn javascript, i'd say it's certainly a valid language and it would belong here. However, that's very rarely the case. Normally javascript is used in a browser to manipulate the DOM, so i'd consider it web programming in the majority of the cases.

-Lee

Edit: this will change the h in html, too. URL processing isn't my specialty.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.