SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why don't this javascript work?

    Why don't this work?

    Code:
    function write(){
    writeOutPath("td");/*Don't work to pass arguments this way?*/
    }
    
    
    function writeOutPath(theSearchWord){
    var theUrlToGoTo = "";
    var currentUrl = window.location.href;
    var aPosition = currentUrl.indexOf(theSearchWord); /*This becomes undefined, why???*/
    
    for(i=0; i< aPosition.length;i++){
    theUrlToGoTo=(theUrlToGoTo +  currentUrl.charAt(i));
    }
    document.getElementById('testning').innerHTML =theUrlToGoTo + aPosition.length;
    /*window.location("www.hulenet.se");*/
    }

  2. #2
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE]
    var aPosition = currentUrl.indexOf(theSearchWord); /*This becomes undefined, why???*/
    [QUOTE]
    That's not undefined, but it returns a Number not an object with a length property. This is what is invalid:
    Code:
    for(i=0; i< aPosition.length;i++){
    Should be

    Code:
    for(i=0; i< aPosition ;i++){
    Probably best to look at the substr() and substring() methods of the String object as an alternative.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •