What options are there to render Javascript "document.write" code serverside with PHP?

136 views Asked by At

The vendor that we are using for a piece of software has an API that returns raw javascript. The intention is to have a script tag with the API as the source. Our smaller pages have 70+ client-side requests to populate the page with all the required data. The response looks something like this:

document.write("\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23719&information_id=38921&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23719&information_id=38921&type=&syndicate=syndicate\" target=\"blank\" \u003eGetting Started with Next Generation Classrooms - Incubator Classroom\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23756&information_id=39010&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23756&information_id=39010&type=&syndicate=syndicate\" target=\"blank\" \u003eGmail and Calendar Tips and Tricks\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23752&information_id=38995&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23752&information_id=38995&type=&syndicate=syndicate\" target=\"blank\" \u003eCreating Accessible and Fillable Electronic Forms\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23720&information_id=38923&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23720&information_id=38923&type=&syndicate=syndicate\" target=\"blank\" \u003eGetting Started with Next Generation Classrooms - General\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv class=\"cal home_events\"\u003e\u003cdiv class=\"cdate blue\"\u003e\u003cp class=\"date\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23881&information_id=39283&type=&syndicate=syndicate\" target=\"blank\" \u003e8/28\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"cinfo home_event_decs\"\u003e\u003ca href=\"http://events.sjsu.edu/EventList.aspx?view=EventDetails&eventidn=23881&information_id=39283&type=&syndicate=syndicate\" target=\"blank\" \u003eComedySportz Improv Show\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e");

As you can see, a DOM structure is being built and printed to the page where it's called. What I would like to do is render the response serverside for a smoother user experience. We're running PHP on our backend. I'm not sure if V8js is a solution - I can't find any examples where a document.write is used or V8js being used outside the context of React.

1

There are 1 answers

0
clearshot66 On

If by this you mean you want to build the js code backend (it'll still load front end though as html pages do with js)

PHP uses echo

so:

echo "whatever you want to output";

or

$output = "<script>$(document.ready(function(){
alert('hi');
});</script>"

echo $output;