• JAVASCRIPT > connaitre l’URL courante

      exemple : http://site.fr:8080/fr/search?q=URL#search-results

       

      window.location.href > http://site.fr:8080/fr/search?q=URL#search-results

      window.location.protocol > http:

      window.location.host > mon.site.fr:8080

      window.location.hostname > mon.site.fr

      window.location.port > 8080

      window.location.pathname > /fr/search

      window.location.search > ?q=URL

      window.location.hash > #search-results

      window.location.origin > http://mon.site.fr

      window.location.username username specified before the domain name.

      window.location.password password specified before the domain name.

       

      En jQuery :

       

      $(location).attr('href');

      méthodes

      .assign() Loads the resource at the URL provided in parameter.

      .reload() Recharge la page. reload(true) = recharge du serveur, sinon recharge du cache.

      .replace() comme assign() mais pas de possibilité de retour en arrière (pas d’historique).

      .toString() synonyme à .href, though it can’t be used to modify the value.

      changer de page

      location.assign("http://ma.page.org");
      location = "http://ma.page.org";

      recharger la page à partir du serveur

      location.reload(true);

      propriétés de l’adresse

      function showLoc() {
        var Loc = location, aLog = ["Property (Typeof): Value", "location (" + (typeof Loc) + "): " + Loc ];
        for (var sProp in Loc){
          aLog.push(sProp + " (" + (typeof Loc[sProp]) + "): " +  (Loc[sProp] || "n/a"));
        }
        alert(aLog.join("\n"));
      }

      envoyer des données

      function sendData (sData) {
        location.search = sData;
      }
      // in html: <button onclick="sendData('Some data');">Send data</button>

       

      The current URL with ?Some%20data appended is sent to the server.

       

 

Aucun commentaire

 

Laissez un commentaire