jQuery question

hi, I have links in my page in the form of this

<a href=“example.php?key=someid” class=“link”>some link</a>

I want to refresh href attribute of link via ajax every five seconds,

I am trying this,

$(function(){ setTimeout(refreshlinks(), 5000);});
                                function refreshlinks(){
                                    $.ajax({ url: "secret_code.php", success: function(){
                                    $('a[class|="link"]').attr('href',function(){
                                        return this.href.replace(/key=.*$/,"key=" + data);
                                    });
                                    }});
                                    setTimeout(refreshlinks(), 5000);
                                }

However, this freezes my browser, and I am not sure if it works at all. Am I doing something wrong, or is it faster way to achive this,

Best regards,

Yaşar Arabacı.

Ohh, and by the way, this is secret_code.php

<?php
echo md5(time());
?>

Edit: I am trying this with my localhost. 1.7 gHz intel mobile processor.

Edit 2: when running this, chrome and apache seems to be using ~= 30% processor each.

Hi Yasar,

Can you post the actual code.

Hmm, actual code is part of a long php file, but I will cut off the unnecessary parts and put actual code here in a minute

This is a little bit edited than my original post. But still doesn’t work.


<!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>
        <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript">function refreshlinks(){
            $.ajax({ url: "secret_code.php", success: function(){
                    $('a[class|="link"]').attr('href',function(){
                        return $(this).attr('href').replace(/key=.*$/,"key=" + data);
                        setTimeout(refreshlinks(), 5000);    
                    });

                }});
        }</script>

    </head>

    <body onload="setTimeout(refreshlinks(), 5000)">

        <div id="page-container">
            <div id="content">
                <div id="gallery">
                    <ul>
                        <?php foreach(explode(";",$product->ek_resim) as $resim):?>
                        <li>
                            <a href="/images.php?id=<?=$product->id . "&&resim=" . $product->ek_resim . "&&key=" . md5(time());?>" class="link">
                                <div class="image">
                                    <img src="/images.php?id=<?=$product->id . "&&resim=" . $product->ek_resim . "&&key=" . md5(time());?>" width="72" height="72" alt=""/>
                                </div>
                            </a>
                        </li>
                        <?php endforeach;?>
                    </ul>
                </div><!-- / gallery -->
            </div> <!--End content-->
        </div> <!--End Container-->
    </body>
</html>

This is secret_code.php


<?php
echo md5(time());
?>

They are both on same folder, if that is related. Direk access to php indicates that it workes fine.

Finally I sorted this out, problem was absence of quotes in the settimeout function, making my code going inside of an endless loop, however, I still have some problems, here is the final code:


        <script type="text/javascript">function refreshlinks(){
            $.ajax({ url: "secret_code.php", success: function(data){
                    $('a[class|="link"]').attr('href',function(data){
                        return $(this).attr('href').replace(/key=.*$/,"key=" + data);
                        setTimeout("refreshlinks()", 5000);    
                    });

                }});
        }</script>

    </head>

    <body onload="setTimeout('refreshlinks()', 5000)">


When I run this, my links end up being, like this:

/images.php?id=1&&resim=resim-2.png&&key=0

Any ideas?

Edit: I sorted it out too, but thanks for reading this topic anyways :slight_smile:

Good to hear that you sorted that out.

/images.php?id=1&&resim=resim-2.png&&key=0

Do you mean why end up with &&?, am i right?

If so, check this line and change it single &.

<a href="/images.php?id=<?=$product->id . "&resim=" . $product->ek_resim . "&key=" . md5(time());?>" class="link">

Also, dont use short tags for PHP, as it will give you headache on many servers. As it is OFF by default in PHP now. You can enable it in php.ini but i will suggest to use full tag like <?php , not just <?. Like in your above code its short tag.

EDIT: Late, I think i was in the writing state while you updated your post. Any how wanna share the solution?

Of course, but problem was not about the double ampersands, it was links weren’t like what they supposed to be, they were just zero,

Reason for the problem was;


$('a[class|="link"]').attr('href',function(data){

This line was overwriting the “data” variable which is sent by $.ajax, I just removed the “data” like this, now it workes perfect :slight_smile:


$('a[class|="link"]').attr('href',function(){

And about the the double ampersand : :smiley:

Final code:


function refreshlinks(){
            $.ajax({ url: "/secret_code.php", success: function(data){
                    $('a[class|="link"]').attr('href',function(){
                        return $(this).attr('href').replace(/key=.*$/,"key=" + data);
                    });
                }});
                setTimeout("refreshlinks()", 1000);
        }

I also want to add that;

I am doing all this bambo jambo to just prevent my images from copied, time-encrypted links, php files as proxy, HTTP referrer checks etc. Sometimes even my site itself can’t display it is own images. But Firefox gives me creeps. Thanks to its “page info” thing, one can easily download images from my site. I even send no-cache, must revalidate header with my images. But firefox … Arrrgghh

To my knowledge there is no way to protect images i mean normal images not water mark images for selling or presentation.
The user who want it will find this way or the other way to get the image.

@OP: You could write a Flash app to present the image gallery, but this will only deter casual leeches, not determined ones. Is it worth your trouble, you have to ask yourself.

I agree, time wasted for you. Everything that comes down to my computer is more or less mine. There will allways be a way to keep it there (you can only make it a bit more difficult, but you can not stop it).

Unfortunately, you are right :slight_smile: Thou I wish there was a attribute as private, like <img src="" private=“true”> that will tell browsers about disabling copy option

That is very very wishfull thinking. Everybody can write browsers, there is no way to “forbid” such a programmer/program to do what it wants with any data it can read from the Internet. Telling browsers what one wants is allways only a suggestion.

Have you ever looked at the program wget?