Refresh to a div

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 it on all pages or I will never know what page the script will run on and in the code above I have to specify an ex page : index.php

    
asked by anonymous 22.06.2015 / 14:01

2 answers

0

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

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

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

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

and then use this name in .load() like this:

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 regex here (link) , which it looks for is a \/ s followed by 1 or more letters by capturing them (\w+) , and followed by .php .

Test the code and tell us if it works the way you want it.

    
22.06.2015 / 16:23
0

An easy way is to assign a variable in the main file (before include), 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";
    
22.06.2015 / 14:49