Ebikeneser
Ebikeneser

Reputation: 2374

Undefined javascript function error

I have made a simple javascript function that gets executed on button click -

<asp:TextBox ID="TextBox3" runat="server" Width="98px"></asp:TextBox>
<asp:Button ID="Button3" runat="server" Text="Button" OnClientClick="fn4();" />

<script  type="text/javascript">

function fn4() 
{

var search = document.getElementById('TextBox3').value;

<iframe src="http://fooBar.com/q=" + search + " width="250" height="400" scrolling="no" frameborder="0"></iframe>

}
</script>

So the search term is based on the user input from TextBox3, however when this is executed, it brings up the error -

Error: 'fn4' is undefined

How can I resolve this?

Upvotes: 0

Views: 1384

Answers (5)

Kshitij
Kshitij

Reputation: 8634

<iframe src="http://fooBar.com/q=" + search + " width="250" height="400" scrolling="no" frameborder="0"></iframe>  

Why is this in your JavaScript function? This will not work

Upvotes: 0

Romain Valeri
Romain Valeri

Reputation: 22057

1) Post your generated html code rather than asp code : it's more or less useless here because we don't know for sure what your asp will generate from this.

2) an HTML tag inside your script section will definitely break the page parsing.

Upvotes: 0

David
David

Reputation: 219047

You're probably getting a parser error in your browser when the page loads. And because the JavaScript isn't parsing, the function isn't defined. This is invalid JavaScript:

function fn4() 
{

    var search = document.getElementById('TextBox3').value;

    <iframe src="http://fooBar.com/q=" + search + " width="250" height="400" scrolling="no" frameborder="0"></iframe>

}

There's HTML mixed in there, so it won't parse as JavaScript. What exactly are you trying to do with that iframe?

Upvotes: 3

Amberlamps
Amberlamps

Reputation: 40498

You cannot have HTML in your JavaScript-Function.

Upvotes: 0

lanzz
lanzz

Reputation: 43198

I'm not familiar with ASP, but you generally cannot mix JS and HTML like you do with the <iframe> in your fn4() function. Your function as it is has a syntax error and thus will not be successfully defined.

Upvotes: 1

Related Questions