0

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.

3
  • I don't understand the question. Commented Apr 2, 2014 at 19:48
  • possible duplicate of How can I get query string values in JavaScript? Commented Apr 2, 2014 at 19:50
  • @amandanovaes . Please see my answer, I think that is what you're after? Commented Apr 2, 2014 at 20:03

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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));
   }   
}

Comments

0

Currently JavaScript don't have any of the built-in function for such purpose. What you're talking about i.e. 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.

3 Comments

using location.search will retrieve the parameters of the page itself not the parameters sent to the script external call.
I actually AM talking about the page parameters. That was a function to get all of the functions and then search for the function passed on.
Ok, I understand, but it's not what I need. Thank you anyway.
0

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

9 Comments

this doesn't answer the question, and actually isn't true: there is nothing stopping you from getting the query string via js...
I know very well PHP but I need to do that with JS. With JS it's easy to retrieve GET parameters using x = window.location.href and then I can parse the parameters. It's very easy.
I updated my post, should solve your problem =) (no downvote please... =D)
but it still does not answer my question. I need to get the parameters sent to the external script. Thank you anyway.
Ok I have another Idea, I'm updating.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.