Refresh to a div

Asked

Viewed 121 times

2

I’m having trouble applying the following Javascript code to my platform:

<script>
    var $scores = $("#refresh");
    setInterval(function() {
        $scores.load("index.php #refresh");
    }, 30000); 
</script>

Because the <div> in question is in a separate file and I include of it on all pages or I will never know which page the script will run on and in the code above I have to specify a page ex: index.php

  • 1

    What doesn’t work? Can you describe the problem?

  • The problem is that since I don’t know what page I’ll be on, scricpt never runs because I have to tell you what the current page is, as you can see in the code load("index.php #refresh");

  • :/ I haven’t figured it out yet. This string you pass to . load() should be different depending on which page it’s on?

  • yes where this index.php must be the name of the page point its extension

  • You can give an example of how the page urls are ?

  • for example home.php , avisos.php

Show 1 more comment

2 answers

0

An easy way is to assign a variable in the main file (before including), then refer to that variable in the included file.

Parent File:

$myvar_not_replicated = __FILE__; // Make sure nothing else is going to overwrite
include 'other_file.php';

Included File:

if (isset($myvar_not_replicated)) echo "{$myvar_not_replicated} included me";
else echo "Unknown file included me";

0


If I understand you want to use this string $scores.load("index.php #refresh") the filename of the page you have in the url.

For example the url/page dominio.com/home.php must cause the .load() fetch "home.php #refresh".

To do this you can use a regular expression by filtering that name:

var nome = window.location.href.match(/\/(\w+)\.php/);
if (nome) nome = nome[0];

and then use that name on .load() thus:

var nome = window.location.href.match(/\/(\w+)\.php/);
if (nome) nome = nome[0];
var $scores = $("#refresh");
setInterval(function() {
    $scores.load(nome + " #refresh");
}, 30000); 

You can see the regex here (link), what she’s looking for is a bar \/ followed by 1 or more letters capturing them (\w+), and be followed by .php.

Test the code and tell me if it works your way.

  • 1

    Yes, it works, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.