Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading Html Into A Javascript Variable

Status
Not open for further replies.

MichaelDPrice

Programmer
Mar 30, 2008
2
US
Hey Guys :)

I have been trying to get this done for hours, and am becoming extremely frustrated!!!!

I am trying to do some Javascript on Demand.

What I am trying to do is get the contents of an html page and turn it into a javascript variable.

I am doing this by dynamically creating a script tag with a cross domain php file which gets the file contents of an html file and then echo'ing the contents into a javascript variable.

Problem is the html is breaking lines and I keep getting the "unterminated string error ...

Does anyone know How I can get this done.
I have tried everyting. I even exploded the html page by singe characters, making the output look like this ...

html = html+'<';
html = html+'d';
html = html+'i';
html = html+'v';
html = html+' ';

html = html+'<';
html = html+'b';
html = html+'r';
html = html+' ';
html = html+'/';
html = html+'>';
html = html+'';

but after the <div />
the nest line will output
html = html+'
';

Then I changed the header to xml
<xmldata>
<thehtml>
.....
......
</xmldata>

But got denied permissions ...

What can I do to access this html pages content, cross domain, via javascript?



 
HA!!!!

BillyRayPreachersSon You Are The Man ....

Im a beginner programmer (although very good at it),

So I dont know much about tabs and line breaks and what not.

It worked great!!

Here is the final code!

THe Local Javascript
--------------------------------------------------------------
function getpage(){
build = document.createElement("script");
build.setAttribute("type", "text/javascript");
build.setAttribute("src", ' document.getElementsByTagName('head')[0].appendChild(build);
//document.getElementById('thepage').innerHTML = html;

}
</script>
------------------------------------------------------------
And The Remote Php 'proxy' code
------------------------------------------------------------
<?php
header("content-type: application/x-javascript");
if($_GET['loadpage']){
$a = file_get_contents($_GET["page"]);
$a = str_replace('"','\"',$a);
$a = str_replace(';','\;',$a);
$a = str_replace(',','\,',$a);
$a = str_replace("'","\'",$a);
//$a = urlencode($a);
$a = preg_replace(
'/([\r\n])/e',
"ord('$1')==10?'\\n':'\\r'",
$a
);
echo "var html;";
echo "html='".$a."';";
echo "document.getElementById('thepage').innerHTML = html";
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top