Javascript Bookmarklet Help

Discussion in 'Web Design and Development' started by whatisthe, May 1, 2009.

  1. whatisthe macrumors member

    Joined:
    Apr 10, 2008
    Location:
    CT
    #1
    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
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You should change the title. Javascript has absolutely nothing to do with real Java: it's just a deceptive marketing name.
     
  3. whatisthe thread starter macrumors member

    Joined:
    Apr 10, 2008
    Location:
    CT
    #3
    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 :)
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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.
     

Share This Page