www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Trouble with indexOf or something...

  1. #1
    Join Date
    Dec 2004
    Posts
    88

    resolved [RESOLVED] Trouble with indexOf or something...

    Hi.

    I need to be able to grab part of a dynamic url in order to display an image (and a corresponding website address).

    The url in the browser can be like:
    Code:
    .../vacancy/application/6614dda7479a472345654d448d9c2d7b/410563/64073?iso=gb
    .../vacancy/application/6214dda7479a472d1561ad4223341d7b/412555/64074
    .../vacancy/application/6631dda7479a472d1561ad4498786678/410559/64075?iso=gb
    The syntax is always the same - starting with /vacancy/application/32-numbers/6-numbers/ and then the part I'm interested in. The ?iso=gb code is not always there.

    How do I grab the numbers in bold?

    I have tried this:
    Code:
    <script language="JavaScript" type="text/javascript">
    var dep = window.location.href.slice(window.location.href.indexOf('?iso') - 5).split(',');
    
    if (dep == "64073?iso=gb")
    {
        var banner = '/files/LB.jpg'
        var url = 'http://www.website1.com'
    }
    else if (dep == "64074?iso=gb")
    {
        var banner = '/files/LT.jpg'
        var url = 'http://www.website2.com'
    }
    else
    {
        var banner = '/files/LK.jpg'
        var url = 'http://www.website3.com'
    }
    </script>
    combined with this bit to display the result:
    HTML Code:
    <script type="text/javascript">                 
    document.write('<a href='+url+' target=\"_blank\"><img src='+banner+' /></a>');
    </script>
    And it actually works - but only if the url ends with ?iso=gb - and this is not always the case.

    So, does anybody have an idea how to solve this? Or is there a smarter way to do this?

  2. #2
    Join Date
    May 2010
    Posts
    213
    Well if the "syntax" is always the same (aka, the numbers always start on the same index), why not use substring?

    The best (and I think only other) way would be using regular expressions, so if this won't work, you better learn regexs
    i love easter eggs
    (if you got any creative easter eggs, send me a PM)

  3. #3
    Join Date
    Dec 2004
    Posts
    88
    Hi.

    Yes, the index is always the same. I'm not really an expert. Can you help me with some code using substring? Or RegEx, if that's the recommended solution? I have no idea where to start. I just copied the code above.

    Regards.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by meek View Post
    I have no idea where to start. I just copied the code above.
    Start by learning JavaScript language. If you don't understand the language, will you understand our advices?

  5. #5
    Join Date
    Dec 2004
    Posts
    88
    I managed to solve it like this:

    HTML Code:
    <html>
    <head>
    function showBanner()
      {
      var dep = window.location.href
      deb = dep.substring(dep.lastIndexOf("/")+1).split("?")[0]
    
      if (dep == "64073")
        {
        var banner = '/files/LB.jpg'
        var url = 'http://www.website3.com'
        }
      else if (deb == "64074")
        {
        var banner = '/files/KB.jpg'
        var url = 'http://www.website4.com'
        }
      else if (deb == "64075")
        {
        var banner = '/files/DSB.jpg'
        var url = 'http://www.website5.com'
        }
      else
        {
        var banner = '/files/default.jpg'
        var url = 'http://www.default.com'
        }
    
      document.write('<a href='+url+' target=\"_blank\"><img src='+banner+' /></a>')
      }
    </head>
    
    <body>
    <div id="banner"><script>showBanner()</script></div>
    </body>
    </html>

  6. #6
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117

    Wink

    Have you defined lastIndexOf? I think it's only available in modern browsers.




    Oh wait, ignore me, I'm thinking of arrays.
    Last edited by NicTlt; 05-06-2010 at 03:28 PM. Reason: brain fart
    Here begynith a techynge of the newe Scrypte with many other helpy thynges, etc

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles