views:

298

answers:

5

Hey everyone,

I have this javascript

function getsomethingelse()
{
    //erzeugt den HTML-Code
    var string;
    string  = "<b>This is a site</b>"

    //beschafft eine referenz auf das <div> element auf der seite
    myDiv = document.getElementById("container");
    //lädt Inhalt in das <div> element
    myDiv.innerHTML = string;
}

wich is linked by this link

<a href="#" onclick="getsomethingelse()" class="panel">Link</a>

I am searching for a way to - instead of writing the whole HTML document into string= - import an external (HTML) file into this; because it is quite chaotic to write a whole HTML document in a single line.

Can someone help me with this? Thanks a lot in advance, benny

+1  A: 

You can Use Ajax for this.

$("#container").load("/neededUrl.html");

Eldar Djafarov
That won't work unless he is using jQuery
Stephen lacy
A: 

1-You can bring the code by ajax 2-If you work with PHP, you can create a <script src="phpjs.php"> and read the contents of the file:

var dataHTML  = <? php echo fileContainerHTML; ?>

3-If you can work with you to make a jQuery jQuery ( "# cont"). Load (...)

andres descalzo
A: 

Yes, use ajax to get the other stuff from your server, then assign that stuff to the innerHTML property.

Eric M
A: 

Yes what you are asking is possible, you can do it using ajax

First of all if you are doing any javascript I recommend using jQuery http://jquery.com/

There is a tutorial for ajax without jquery here: http://www.w3schools.com/Ajax/Default.Asp

Here are some jQuery Tutorials: http://docs.jquery.com/Tutorials

A simpler way to achieve the same thing may be to use an iframe which you simply point at whatever html page, it also means you don't have to strip out the html and body tag.

Also you could have multiple divs with the html you need and show/hide the one(s) required.

Good luck

Stephen lacy
A: 

thanks a lot to everyone for the answers! i am really sorry for the late reply, but somehow the email notification did not work for me :) I did not have any eduction in web design yet, i am starting with it in about 3 weeks, so please excuse my "noobish" questions. I will try this as soon as i am back home and have access to the server. greetings, benny

benny