2010-08-30 3 views
1

donc, mon URL est example.com/?ref=personjavascript: l'analyse des variables dans l'URL

mais ref est nul après la regex. Qu'est-ce que je fais mal ici?

function getReferer(){ 
    var regex = new RegExp(/ref=(.+)/); 
    var ref = regex.exec(window.location.ref); 
    alert(ref); 
    if (ref == null) return ""; 
    else return ref[1]; 
} 

Répondre

1

Remplacer window.location.ref par window.location.href. N'utilisez pas new RegExp si ce n'est pas nécessaire, c'est plus lent.

function getReferer(){ 
    var regex = /ref=(.+)/; 
    var ref = regex.exec(window.location.href); 
    alert(ref); 
    if (ref == null) return ""; 
    else return ref[1]; 
} 
Questions connexes