0

What I want this code to do is display a button called Map. And above that some text should read "Chose a display". Then once that 'Map' button is clicked, it then displays a google map image through the tags.

<html>
<head>
<title>Random</title>
</head>
<body>
<p align="center"style="font-family:verdana;"id="googlemap">
Choose a Display
</p>
<script>
function mapFunction()
{
x=document.getElementById("googlemap"); // Find the element
x.src="<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" https://www.google.com.au/maps?f=d&amp;source=s_d&amp;saddr=23+Burgundy+St,+Carseldine+QLD&amp;daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&amp;hl=en&amp;geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&amp;aq=0&amp;oq=Narr&amp;sll=-27.422699,153.02411&amp;sspn=0.004819,0.008272&amp;t=h&amp;mra=ls&amp;ie=UTF8&amp;ll=-27.555764,153.208466&amp;spn=0.584401,0.878906&amp;z=10&amp;output=embed"></iframe>; // Change the content
}
</script>
<button type="button" onclick="mapFunction()">Map</button>
</body>
</html>
asked May 17, 2013 at 6:15
1
  • 2
    I think you mean to use x.innerHTML='iframe stuff'; Commented May 17, 2013 at 6:17

4 Answers 4

5

I think you mean innerHTML

var x = document.getElementById("googlemap"); // Find the element
x.innerHTML = '<iframe src="url here" ...></iframe>';

Since the HTML contains ", you can either escape them with \ or just wrap the entire string with single quotes '.

Also, you forgot the src for the iframe.

answered May 17, 2013 at 6:17
Sign up to request clarification or add additional context in comments.

1 Comment

There are a few more problems with the iframe tag, see my answer for a fixed string.
0

Two things:

  • You do not want to change the src of x, instead try modifying the innerHTML.

  • Your iframe tag is both malformed (the URL should be prefixed with src=" and there is no such thing as a marginwidth or marginheight attribute) and is not contained properly within the string (all dublequotes should be escaped \"like this\")

So, this should work:

function mapFunction()
{
 x=document.getElementById("googlemap"); // Find the element
 x.innerHTML = "<iframe width=\"640\" height=\"480\" frameborder=\"0\" scrolling=\"no\" style=\"margin-height:0;margin-width:0;\" src=\"https://www.google.com.au/maps?f=d&amp;source=s_d&amp;saddr=23+Burgundy+St,+Carseldine+QLD&amp;daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&amp;hl=en&amp;geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&amp;aq=0&amp;oq=Narr&amp;sll=-27.422699,153.02411&amp;sspn=0.004819,0.008272&amp;t=h&amp;mra=ls&amp;ie=UTF8&amp;ll=-27.555764,153.208466&amp;spn=0.584401,0.878906&amp;z=10&amp;output=embed\"></iframe>"; // Change the content
}
answered May 17, 2013 at 6:20

Comments

0

A p-element can never have an src-attribute. Or you create the iframe with an empty src value / or you use x.innerHTML to write the iframe in.

As others said before, you need to \"escape the quotes\"

answered May 17, 2013 at 6:21

Comments

0

It's because, one you need to use innerHTML , and two, you need to not close your quotes when setting innerHTML

x.innerHTML='<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://www.google.com.au/maps?f=d&amp;source=s_d&amp;saddr=23+Burgundy+St,+Carseldine+QLD&amp;daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&amp;hl=en&amp;geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&amp;aq=0&amp;oq=Narr&amp;sll=-27.422699,153.02411&amp;sspn=0.004819,0.008272&amp;t=h&amp;mra=ls&amp;ie=UTF8&amp;ll=-27.555764,153.208466&amp;spn=0.584401,0.878906&amp;z=10&amp;output=embed"></iframe>';
answered May 17, 2013 at 6:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.