[Javascript] Functie werkt niet met document.referrer?

Pagina: 1
Acties:

  • Reveller
  • Registratie: Augustus 2002
  • Laatst online: 05-12-2022
Ik heb het volgende javascriptje:

HTML:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script language="javascript">

function urlVariables(theURL, strVar){

    url = theURL.search.substr(1).split("?");
    urlVar = new String(url);
    arrUrl = urlVar.split("&"); 

    for(x=0; x<arrUrl.length; x++){
        arrU = arrUrl[x].split("=");

        if (arrU[0] == strVar){
            intVar = arrU[1];
            x = arrUrl.length;
        }
    }
    return(intVar);
}

p = parseInt(urlVariables(document.location, 'id'));
r = parseInt(urlVariables(document.referrer, 'id'));

alert(p+"\n"+r);

</script>


...
hierbij is document.location: http://localhost/website/index.php?id=58
en document.referrer: http://localhost/website/index.php?id=23


Het probleem is, dat dit script mij voor p wel 58 retourneert, maar een error genereert voor r: "urlVariables is undefined". Dit snap ik niet, want als ik zeg:

HTML:
1
2
3
4
5
... zelfde meuk als boven ...

r = document.referrer;

alert(p+"\n"+r);


krijg ik wel gewoon http://localhost/website/index.php?id=23 retour. Wat doe ik voud?!

[ Voor 7% gewijzigd door Reveller op 16-05-2004 18:51 ]

"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."


Verwijderd

search Property applies to: A, AREA, location

  • Reveller
  • Registratie: Augustus 2002
  • Laatst online: 05-12-2022
Dank je. Maar heeft iemand dan een idee hoe ik alsnog de id uit document.referrer kan filteren?

"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."


Verwijderd

Leg even uit wat het volgende stukje doet
code:
1
2
3
url = theURL.search.substr(1).split("?");
urlVar = new String(url);
arrUrl = urlVar.split("&");

Ik weet het trouwens wel, maar probeer maar even zelf uit te leggen wat er gebeurt en waarom dat gebeurt.

  • Reveller
  • Registratie: Augustus 2002
  • Laatst online: 05-12-2022
1. de querystring wordt van de url gescheiden: met http://localhost/website/index.php?id=58 als voorbeeld krijg je url = 'id=58'
2. deze querystring wordt op '&' ge-explodeerd. Deze 'stukjes' worden in de arrURL array bewaard

Volgens mij klopt dit gewoon :? Zoals Jorgen al zei, search werkt alleen voor A, AREA en location. Dus daarbuiten klopt de code toch?

Ik weet nu alleen niet hoe ik verder moet...

"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."


Verwijderd

Ik ga even dom verder vragen hoor. Omdat ik vind dat je best zelf een beetje mag proberen.
Waarom staat er theURL.search.substr(1).split("?") en niet theURL.substr(1).split("?")

  • Reveller
  • Registratie: Augustus 2002
  • Laatst online: 05-12-2022
@Cheatah. Van een beetje proberen is nog nooit iemand dood gegaan :D. Het duurde even, maar nu werkt het wel...

HTML:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function get(theURL, theVar) {
    var str_us = theURL.split("?");
    var str_qs = str_us[1];
    var arr_qs = str_qs.split("&");

    var urlsplit = theURL.split("?");
    var qs       = urlsplit[1];
    var qsa      = qs.split("&");

    for (i = 0; i < arr_qs.length; i++) {
        arr_pair = arr_qs[i].split("=");
        
        if (arr_pair[0] == theVar) {
            theVal = arr_pair[1]
        }
    }
    return theVal;
}

[ Voor 11% gewijzigd door Reveller op 16-05-2004 20:51 ]

"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."

Pagina: 1