0

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?

3
  • you could try javascript:fn4(); instead of fn4(); Commented Jun 13, 2012 at 9:00
  • Remove that iframe thing? Whatever it was supposed to do, it's a syntax error - no function to be declared. Commented Jun 13, 2012 at 9:00
  • Or try var fn4 = function() {} Commented Jun 13, 2012 at 9:01

5 Answers 5

3

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?

Sign up to request clarification or add additional context in comments.

4 Comments

Within the iframe is a url that shows search results. I am just trying to take a users seach term and put it into the url then run the search.
@Jambo: But what do you expect this to do when the JavaScript function is called? You should treat the entire line as a string literal in JavaScript. Then maybe you want to write it to a specific location on the page? Or return it from the function and the calling code can write it to the page? The latter is probably a bit cleaner.
I am having difficulty trying to get the users search term and inserting it into the iframe url then displaying it on the page. How would you suggest I do this?
@Jambo: To actually write the string literal to the page, you'll probably want to take a look at document.write() - developer.mozilla.org/en/document.write
1

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.

Comments

0

You cannot have HTML in your JavaScript-Function.

Comments

0

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.

Comments

0
<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

Comments

Your Answer

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.