Czy istnieje jakaś metoda, aby uzyskać adres URL bez ciągu zapytania?

Mam URL jak http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235.

Chcę uzyskać adres URL bez ciągu zapytania: http://localhost/dms/mduserSecurity/UIL/index.php.

Czy jest na to jakaś metoda w JavaScript? Obecnie używam document.location.href, ale zwraca pełny adres URL.

Author: Xufox, 2011-04-28

11 answers

Spróbuj tego: window.location.href.split('?')[0]

 255
Author: tradyblix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-28 11:01:49

Przeczytaj o Window.location oraz Location Interfejs:

var url = [location.protocol, '//', location.host, location.pathname].join('');
 304
Author: Felix Kling,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-29 05:09:03
location.toString().replace(location.search, "")
 27
Author: Quentin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-28 11:02:40
var url = window.location.origin + window.location.pathname;
 7
Author: Jason,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-07 15:32:21

Try:

document.location.protocol + '//' +
document.location.host +
document.location.pathname;

(NB: .host zamiast .hostname, aby port również został włączony, jeśli to konieczne)

 5
Author: Alnitak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-28 11:05:02

Jeśli chcesz również usunąć hash, spróbuj tego: window.location.href.split(/[?#]/)[0]

 5
Author: user1079877,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-14 05:57:17

Po prostu Wytnij łańcuch za pomocą Splitu (w łatwy sposób):

var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);
 2
Author: pleasedontbelong,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-28 11:03:24

Aby pobrać każdą część adresu URL z wyjątkiem zapytania:

var url = (location.origin).concat(location.pathname).concat(location.hash);

Zauważ, że obejmuje to również hash, jeśli taki istnieje (wiem, że nie ma hasha w Twoim przykładowym adresie URL, ale uwzględniłem ten aspekt dla kompletności). Aby wyeliminować hash, po prostu wyklucz .concat(location.hash).

Lepiej jest używać concat do łączenia łańcuchów Javascript (zamiast +): w niektórych sytuacjach unika się problemów, takich jak pomyłki typu.

 2
Author: Andrew Faulkner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-17 15:20:39

Oto dwie metody:

<script type="text/javascript">
    var s="http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu
                                =true&pcode=1235";

    var st=s.substring(0, s.indexOf("?"));

    alert(st);

    alert(s.replace(/\?.*/,''));
</script>
 1
Author: TheVillageIdiot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-28 11:04:05

Co powiesz na to: location.href.slice(0, - ((location.search + location.hash).length))

 1
Author: LI XiangChen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-01 20:17:22

Użyj właściwości window.location

var loc = window.location;
var withoutQuery = loc.hostname + loc.pathname;
var includingProtocol = loc.protocol + "//" + loc.hostname + loc.pathname;

Możesz zobaczyć więcej właściwości w https://developer.mozilla.org/en/DOM/window.location

 0
Author: detaylor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-28 11:03:26