0

I was wondering if there was some way to get a javascript variable from another url into a variable within PHP?

For example:

A page called exampleurl.com/page (which I do not have control over) with some inline javascript like:

function examplefunction() {
    evar = "http://ikmp.co/Usdfio1";
}

Assuming there's HTML and other javascript surrounding this variable I want, how would I be able to retrieve it? Is there a special function to do this, or would it be easier to simply parse the HTML through PHP and then trim the variable to what I want?

In theory, the PHP would have a URL set to retrieve the JS from, and it would echo only what is in that variable.

PHP code (hypothetical) at mysite.com/getvariable.php:

$url = 'exampleurl.com/page';
$js_var =  get_js_data($url, $evar);
echo $js_var;

Thanks in advance to anyone who can help me out here!

1
  • @Mrdoctor Kovacic: in other words, you'd like to parse text contents of a page to extract anything resembling javascript? Specifically anything in between say "function examplefunction() {" text and a matching closing curly bracket? Commented Aug 31, 2013 at 3:37

1 Answer 1

1

how about this:

HTML FILE:

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <div id="result"></div>
    <script> 
    var javascriptVar ='pie'; //this is whatever the js variable you want it to be

    $('#result').load('path/to/my/myphpfile.php?var='+javascriptVar );
    </script>

myphpfile.php

<?php
$javascriptVar =$_GET['var'];

echo $javascriptVar;
?>

notice the php code HAS to be in a seperate file than the html for this to work

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

2 Comments

Thanks, but I don't have control over the JavaScript file, it's from another website.
you dont have to alter the external script. my js code can be on a different file on your server that simply retrieves the variable from the other website

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.