0

I have the following JS:

<script>
   function SetContent(link) {
            document.getElementById('lowerContent').innerHTML =
         "<iframe frameborder='0' src='" + link + "' width=100%' height='" + (screen.height - 100) + "px'";
        } 
</script>

Which gets called like so:

<a href="javascript:SetContent('http://urlhere);" title="URL">URL</a>

How can I make the <a> execute on button click? Or how to assign "javascript:SetContent('http://urlhere);" to <input type=button>

No postback is required. The click simply resets the main div and adds an iframe with the supplied URL. Works fine for <a> but not sure how to move that to a button.

onclick just causes postback and nothing else.

4 Answers 4

2

Assigning javascript to button clicks works like this:

<input type="button" onclick="javascript:SetContent('http://urlhere);" value="Click me at your own risk!">

adjust attributes (id, etc.) accordingly.

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

2 Comments

Yup that's it. One small issue as I have a few other asp:buttons around the page, when I enter text in a textbox and hit enter it activated the other buttons not just the one above. How to fix that?
I encourage you to ask another question about it, providing the relevant bits of code (both ASP and C#).
2
<input type="button" onclick="SetContent('http://urlhere');" />

Comments

0

try this. Use onclick instead of href.

<a href="#" onclick="javascript:return SetContent('http://www.google.com');" title="URL">URL</a> 

Or

<a href="javascript:void(0);" onclick="javascript:return SetContent('http://www.google.com');" title="URL">URL</a> 

Comments

0

In the script you have set return false. Due to this, it is returning true and calls postbacks. You dont need to add javascript on href. you can add onclick as follows:

<input type="button" onclick="SetContent('http://urlhere');" />

<script>
    function SetContent(link) 
    {
        document.getElementById('lowerContent').innerHTML = "<iframe frameborder='0' src='" + link + "' width=100%' height='" + (screen.height - 100) + "px'";
        return false;
    } 
</script>

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.