View Single Post
Old 04-22-2008, 06:26 AM   #3 (permalink)
xrvel
Platinum Lifetime Member
 
xrvel's Avatar
 
Name: Kurniawan
Last Online: Today 08:16 AM
iTrader: (0)
Join Date: Nov 2007
Posts: 17
DNF$: 330
Location: Indonesia
Country:


Quote:
Originally Posted by sunja View Post
you could also try using document.title instead of all that split/pop/shift stuff:

var picsource = document.title;

EDIT: it might be "window.title" actually, not sure. one of those two.
The document.title is the correct one.

If a document have no title, the document URL is displayed in title section, but, the JavaScript still considers the title as empty, therefore, we must check it based from the document URL. Here's the full code.
PHP Code:
<script type="text/javascript">
function 
setImage(imageId) {
    var 
docTitle document.title '';
    var 
docUrl document.location '';
    var 
picSource '';

    
picSource docUrl.split('/').pop().split('.').shift();
    
picSource picSource +'.jpg';
    
alert('Pic Source = ' picSource);
    if (
document.getElementById(imageId)) {
        
document.getElementById(imageId).src picSource;
    } else {
        
alert('Image not found');
    }
}
</script>

<img src="a.jpg" id="your-image-id" />
<input type="button" value="Change Image" onclick="setImage('your-image-id')" /> 

Last edited by xrvel; 04-22-2008 at 06:44 AM. Reason: Automerged Doublepost
xrvel is offline   Reply With Quote