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.

amandanovaes
  • 694
  • 2
  • 7
  • 20

4 Answers4

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.

mituw16
  • 5,126
  • 3
  • 23
  • 48
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));
   }   
}
Łukasz Szewczak
  • 1,851
  • 1
  • 13
  • 14
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

Community
  • 1
  • 1
Apolo
  • 3,844
  • 1
  • 21
  • 51
  • this doesn't answer the question, and actually isn't true: there is nothing stopping you from getting the query string via js... – benomatis Apr 02 '14 at 19:50
  • 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. – amandanovaes Apr 02 '14 at 19:50
  • I updated my post, should solve your problem =) (no downvote please... =D) – Apolo Apr 02 '14 at 19:57
  • 1
    but it still does not answer my question. I need to get the parameters sent to the external script. Thank you anyway. – amandanovaes Apr 02 '14 at 19:59
  • Ok I have another Idea, I'm updating. – Apolo Apr 02 '14 at 20:03
  • Feel free to tell me if it solved your problem or not. – Apolo Apr 02 '14 at 20:10
  • @Apolo I thank you your effort but it still does not answer my question. The call is made to an external resource and whenever I use window.location it will give me back the page itself not the script url call. – amandanovaes Apr 02 '14 at 20:11
  • You are coding teste1.php ? Not sure I understand. Because my second edit is no more about using window.location =) – Apolo Apr 02 '14 at 20:14
  • You are right but your code uses PHP and I cannot use PHP in this particular case. @mituw16 presented a really nice answer. It's a creative one but works fine in my case! Thank you anyway for trying help me! – amandanovaes Apr 02 '14 at 20:17
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.

Community
  • 1
  • 1
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103