0

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>
2
  • Is your main issue that you don't want to have to physically create each HTML file? (1.html, 2.html, 3.html etc..) It seems like this would be a much more appropriate job for a PHP script so that the URLs are loaded on to the page rather than manipulating them after the page has already loaded. Commented Jan 21, 2012 at 8:26
  • I made an index.php for yeah, let me know if it works, I can't test it. I'll update it if it doesn't. Commented Jan 21, 2012 at 8:50

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

that still not an answer, because I do not know how to get the the number of the current page covert it to a number, store it into a variable then use it as a src for the image.
is your page number coming from query string?
im sry mayb im nt getting it.. but arnt you getting the image numbers by incrementing them.. is your problem is getting the number of the firsr image tht is to loaded.. if yes you can pass tht one value in the query string.. i supoose you already hav your image names in the proper sequence of 1, 2, 3.. etc etc
I am increasing the number of the current url of the website. then it will to to the next page.
I think this is what Parv means, though it's not much more compact than your way of doing it. This keeps the user on the page but changes the src of the image. If you wanted to change pages you'd set the window.location in the last line of the function instead of the src of the image. jsfiddle.net/andfinally/XQV33
|
1

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!

Comments

0

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>

6 Comments

well did you look at my website ? I just do not want to rename every thing every time I want to update the website.
@MaanSahir, Alright, try that now. Sorry, it might take a couple before I get it, I'm used to constant trial and error on my own servers.
@MaanSahir, this one for sure... I hope
Can I use this, it is not woking can you fix it ? <script> var imageURL = return(url.replace(/(\d+)(\.html)$/, function(str, p1, p2) { return((Number(p1)) + p2); </script> <img src="Images/'+ imageURL +'.jpg">
@MaanSahir, Sorry man, the one I have up now should work, but when you tried it, it gave an error. If you try it again, it should print out the document root so I can make sure it's the right place. I don't think this is going to work though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.