简体   繁体   中英

Get URL parameters with Javascript

Hi I have the following tag inside my HTML:

<script type="text/javascript" src="http://XX/teste1.php?BLABLABLA"></script>

Is there somewhay inside teste1.php, using JS to retrieve the parameters BLABLABLA? If I use window.location.href I get the index.php location (of course) but I need to get the parameters sent to the external resource using JS and not PHP.

I think I understood what you are after. Take a look the following fiddle.

http://jsfiddle.net/gK58u/2/

You can see I'm manually loading in jQuery, and then getting the src from the script declaration.

===================

HTML Add an id to your script declaration

<script id="jquerysrc" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js?key=test"></script>

Javascript

$(document).ready(function() {
    var scriptsource = "";
    scriptsource = $("#jquerysrc").attr("src");
    alert(scriptsource);
});

This will allow you see the url that your external js file is coming from. This is more a point in the right direction.

You can try this, without using jQuery.

var scripts = document.getElementsByTagName('script'),i,src;

for(i=0;i<scripts.length;i++){
   src = scripts[i].src;
   if (src && src.indexOf('?')>=0){
       console.log(src.substring(src.indexOf('?')+1));
   }   
}

It seem you don't understand how PHP and JS works together. Php generate HTML (maybe JS and CSS too). Then when this html is loaded by a client, JS is executed.

To get it in PHP or JS, you can use regex or modify the url from ?BLABLABLA to ?key=BLABLABLA . In PHP, "BLABLABLA" will be stored in $_GET['key']


EDIT :

well I misunderstood your question.

From " How to retrieve GET parameters from javascript? " :

-------------------------

With the window.location object. This code gives you GET without the question mark.

window.location.search.replace( "?", "" );

-------------------------

From your example it will return BLABLABLA

window.location DOC


EDIT2 :

When you generate your Javascript in teste.php, you should do this :

$str = "";
foreach ($_GET as $key => $value) {
    $str = $key;
}
echo "var getParam = ".$str.";";

I don't see how to avoid foreach if you don't know what is given. You may have to rebuilt the parameters string ("?xxx=sss&ddd=zz...")

Now you JS variable getParam should contains BLABLABLA

Apolo

Currently JavaScript don't have any of the built-in function for such purpose. What you're talking about ie BLABLABLA is known as Query String . Which is attacked to the URL to make a dynamic web page (change the content depending on the condition)

First Method

Is to get the whole URL, and then replacing the URL with empty string and so on to get only the last element.

Second method

Another answer was posted with a function (custom function, not a builtIn function). In which you pass on a parameter to the method, which gets the parameter values for you. It is easy to understand too.

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

To call the function, you use getParameterByName('prodId') . Use it inside a function or variable and you're good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM