Can't get an easy javascript code to work

Discussion in 'JavaScript' started by shadow_boi, Jul 17, 2008.

  1. #1
    Sorry, I dont know much about java script.

    what i want is to replace the url 1.jpeg to 1-thumb.jpg

    but i cant get it working. please help. Thanks.

    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <script type="text/javascript">
    function getthumb(url)
    {
    	var alink = url;
    	var parts = alink.split("."); //split using dot as delimiter
    	
    	document.write(parts[0] + "-thumb.jpg");
    }
    </script>
    
    </head>
    
    <body>
    <img src="javascript:getthumb('1.jpeg');">
    </body>
    
    PHP:
     
    shadow_boi, Jul 17, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    function getthumb(url)
    {
    	return url.replace(/(\d+)\.[a-z]{1,4}/, '$1-thumb.jpg');
    }
    
    Code (javascript):
     
    nico_swd, Jul 19, 2008 IP