Maan Sahir
Maan Sahir

Reputation: 369

image src from url?

in my website I am using the function bellow to navigate through images.(http://jsut4fun.site40.net/2.html) the function adds 1 to the url so that it will go to the next page. as you can see I have to rename each webpage and image then I have to change the code of the webpage for each image number. is there any easy way to store the number from the url in the src of the image so that I do not have to change the code ? generally do you have any ideas that can help me do this better ? I still a javascript noob :)

<img src="Images/100.jpg">     
<script>
        var url = location.href;
        function nextImage() {
            return(url.replace(/(\d+)(\.html)$/, function(str, p1, p2) {
                return((Number(p1) + 1) + p2);
            }));
        }
        function prevImage() {
            return(url.replace(/(\d+)(\.html)$/, function(str, p1, p2) {
                return((Number(p1) - 1) + p2);
            }));
        }
        </script>
          <a href="javascript: window.location.href = prevImage();"> <img border="0" src="Assets/Left.png" /></a>
          <a href="javascript: window.location.href = nextImage();"> <img border="0" src="Assets/Right.png"/></a>

Upvotes: 0

Views: 4416

Answers (3)

And Finally
And Finally

Reputation: 5714

Maan, I think I understand now what you want to do. You want to be able to reset the src of the image on all your numbered pages without having to change it manually. This is doable with Javascript, though I'm not sure it's strictly advisable, because it means the image will be wrong for browsers which don't have JS enabled. That's a very unusual case, but it might include Googlebot - so if it's important that the image is correct for SEO purposes you probably have no alternative but to change all your image filenames.

For what it's worth, this JS should do what you want.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>

    $(document).ready(function () {

        var currentPage = window.location.href.split('/');
        var currentNum = currentPage[3].split('.')[0];
        $('img').attr('src', 'Images/' + currentNum + '.jpg');

        $('#prev').click(function () {
            move(-1);
        });

        $('#next').click(function () {
            move(1);
        });

        function move(n) {
            window.location.href = Number(currentNum) + Number(n) + '.html';
        }

    });

</script>

This bit includes JQuery in your page

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

The bit inside $(document).ready(function () { ... } is run when the page is ready to display, usually before it's loaded.

var currentPage = window.location.href.split('/'); gets the current URL of the page and splits it at each / character. This line produces an array which contains the parts of the URL. If your URL is http://jsut4fun.site40.net/5.html, "5.html" will be the fourth part, which is located at index position [3] of the array.

var currentNum = currentPage[3].split('.')[0]; grabs the fourth part of the URL and splits it at the '.' This gives us the page number.

$('center img').attr('src', 'Images/' + currentNum + '.jpg'); resets the src attribute of your main image to the right URL for the page. 'center img' selects any image which is a child of a center element.

You could keep your existing links and prevImage() and nextImage() functions to move between pages, but if you wanted to do this with JQuery as well you could do it with the bit that says

$('#prev').click(function () {
    move(-1);
});

$('#next').click(function () {
    move(1);
});

function move(n) {
    window.location.href = Number(currentNum) + Number(n) + '.htm';
}

This example assumes the links have the ids "prev" and "next". It should be easy enough to add ids to your current links on every page with a global search and replace.

Hope this helps!

Upvotes: 1

mowwwalker
mowwwalker

Reputation: 17392

You would use PHP for something like this. I'm not sure what exactly it is you're trying to do though.

edit:

I wrote a simple php page for you. Save it as "index.php" and put it in your public_html. I'm curious to see if it works:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="just 4 fun, funny, comics, image, photo, humor, joke, pics, picture, fail, epic, fun" />
<title>Just 4 Fun</title>
<style type="text/css">
#Thewholewebsite {
    left:357px;
    top:77px;
    width:1080px;
    height:2000px;
    z-index:1;
    margin-top: 0px;
    margin-right: auto;
    margin-left: auto;
    background-color: #DDD;
    background-image: url(Assets/ContentBC.png);
}
.Bottomad {
    height: 60px;
    width: 468px;
    margin-right: auto;
    margin-left: auto;
}
.share {
    height: 60px;
    width: 714px;
    margin-right: auto;
    margin-left: auto;
}
.Content {
    float: left;
    height: 1850px;
    width: 714px;
    margin-top: 20px;
    margin-left: 15px;
}
.Facebookcomments {
    height: 1000px;
    width: 714px;
}
.Sidead {
    float: right;
    height: 280px;
    width: 336px;
    margin-top: 20px;
    margin-right: 15px;
}
.Facebooklikebox {
    float: right;
    height: 600px;
    width: 336px;
    margin-top: 20px;
    margin-right: 15px;
}
.Header {
    background-image: url(Assets/HeaderLogo.png);
    height: 90px;
    width: 335px;
    margin-top: 5px;
    margin-left: 10px;
    float: left;
}
.Headerad {
    background-color: #000;
    height: 90px;
    width: 728px;
    margin-right: 6px;
    float: right;
    margin-top: 5px;
}
.Facebookcomments {
    height: 1000px;
    width: 714px;
}
</style>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-28177073-1']);
  _gaq.push(['_setDomainName', '.site40.net']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>

<body background="Assets/MainBC.png">
<div id="Thewholewebsite">
<div class="Header"></div>
<div class="Headerad"></div>
<div class="Content">
<center>
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=324524777119";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>


<br/>
<br/>
    <br/>
    <br/>
    <?PHP 
    $prev=$next=true;
    if(isset($_GET['image'])){
        $imgnum = $_GET['image'];
        if($imgnum==1){
            $prev=false;
        }
    }
    else{
        $imgnum=1;
        $prev=false;
    }
    echo "Root: ".$_SERVER['DOCUMENT_ROOT'];
    if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/public_html/Images/'.$imgnum.'.jpg')){
        $imgnum=1;
        $prev=false;
    }
    if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/Images/'.$imgnum+1 .'.jpg')){
        $next=false;
    }


    ?>
<img src=<?PHP echo "\"Images/".$imgnum.".jpg\"" ?>/>
<br/>
<script>
var url = location.href;
function nextImage() {
    window.location.href = 
}
function prevImage() {
    return(url.replace(/(\d+)(\.html)$/, function(str, p1, p2) {
        return((Number(p1) - 1) + p2);
    }));
}
</script>
<?PHP if($prev){?>
        <a href=<?PHP echo "\"".$_SERVER['DOCUMENT_ROOT'].'/Images/'.$imgnum-1 .'.jpg"' ?>> <img border="0" src="Assets/Left.png" /></a>
    <?PHP } if($next){?>
        <a href=<?PHP echo "\"".$_SERVER['DOCUMENT_ROOT'].'/Images/'.$imgnum+1 .'.jpg"' ?>> <img border="0" src="Assets/Right.png"/></a>
    <?PHP }?>
<br/>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-6477782079941355";
/* Just 4 fun */
google_ad_slot = "0518220826";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<br/>

<a name="fb_share"></a> 
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" 
        type="text/javascript">
</script>
<g:plusone></g:plusone>
<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>
<a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<br/>
<div class="fb-comments" data-href="http://jsut4fun.site40.net/9.html" data-num-posts="10" data-width="714"></div>
</center>
</div>



<div class="Sidead"><script type="text/javascript"><!--
google_ad_client = "ca-pub-6477782079941355";
/* dam this is funny ads */
google_ad_slot = "7487699411";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<div class="Facebooklikebox">
<div class="fb-like-box" data-href="http://www.facebook.com/pages/Just-For-Fun/301627143220739" data-width="336" data-height="600" data-show-faces="true" data-stream="true" data-header="true"></div></div>
</div>

</div>
</body>
</html>

Upvotes: 0

Parv Sharma
Parv Sharma

Reputation: 12705

use jquery
then select the image by using $("img") or by $("#Id_OF_Img_Tag") then just change the $("img").attr("src") value.
incrementing or decrementing would be easy as

Upvotes: 1

Related Questions