Possible Duplicate:
Server side browser that can execute JavaScript
Execute javascript in PHP
How can I parse HTML that includes JavaScript code, preferably with PHP script if possible.
As an example:
<a href="javascript:link(10, true);">link</a>
should be replaced by the appropriate value the JavaScript function returns, e.g.
<a href="http://www.example.com">link</a>
A more complex example would be a saved facebook html page which is littered with loads of javascript code.
Summarized: Return a DOM for a page with html+javascript
3 Answers 3
You could just give this link an ID. Not that this solution is javascript, jQuery.
So give the link an Id, or class.
$('.link').each(function() {
var functionName = 'link';
var start = $(this).attr('href');
remove = start.replace('javascript:', ''),
get = remove.replace(new RegExp('^'+functionName+'\(((.+,円?)+)\)\;?', 'g'), function(a, b, c) {
return c.replace(/[()']/g,'')
}), args = get.split(",");
//read settings
var firstArgument = args[0];
$(this).attr('href', firstArgument)
});
Please note this is just an example.
Usage:
<a class="link" href="javascript:link('http://facebook.com')">Hi</a>
That would make the actual link http://facebook.com.
Adding new arguments this way is difficult though and its not really professional. But this should do what you want, I just didn't know what your link function actually doesnt so I didnt add the argument with the boolean. Of course this could get far more complex and you could write a function that could do this too but I just wrote this for your really quick.
Check out the example.
8 Comments
link() didn't take any params this code would reutrn nothing as shown in the users own example: <a href="javascript:link(10, true);">link</a>href attribute of a link with it's param being that of the target link? One reason could be for outbound proxies to protect users from malicious content (`l.php' in this case for fb) but then this function still wouldn't return that proxied link but instead the initial potentially unsafe param that was entered. That's why I don't really see how this answer would work.You can't. When clicked, javascript: URIs just call the function and let it do whatever it wants (which MIGHT include navigating somewhere); they don't expect or use a return value. In many use cases, the function may not cause any navigation at all.
4 Comments
DomDocument can be used to parse HTML in PHP including JS: http://php.net/manual/en/class.domdocument.php
You can "render" the JS with the HTML by merely echoing out the output of a cURL or wget (or whatever you use) without escaping the HTML characters. For external JS you are going to need to build a crawler which will crawl the DomDocument script tags and fetch the appropiate URL and load it into a position that is accessible unless you use it directly from their servers but I don't think they will be too happy about that.
Edit: My new answer after some comments is: no
12 Comments
Explore related questions
See similar questions with these tags.
linkfunction uses the user's mouse coordinates to decide what link to make?