开发者

How to set src in iframe using Javascript

I'm working on a MVC project right now and the current feature being developed is a DropDownList with each option tied to a URL. When the user selects an item and presses Submit, the expected behaviour is for the page to load an iframe on the same page with the particular URL tied to their selection displayed in said frame.

Controller:

Function Configuration() As ActionResult

        Dim configList As List(Of String) = New List(Of String)
        configList.Add("10GBaseLX4")
        configList.Add("10GFC")
        configList.Add("10GigE")
        configList.Add("100BaseFX")

        ViewData("cprotocols") = New SelectList(configList)
   开发者_运维问答     Return View()

    End Function

    Public Function Handle(ByVal cprotocols As String) As String
        Return "http://www.configlist.com/" + cprotocols + "_Config.xml"
    End Function

View:

<% Using (Ajax.BeginForm("Handle", New AjaxOptions With {.OnComplete = "showConfig"}))%>
    Select a configuration file to view: <%= Html.DropDownList("cprotocols") %>
        <br />

        <input type="submit" value="Submit" />
        <br />
        <%End using %>
        <br />
        <script type="text/javascript">
        function showConfig(cprotocols)
        {
            var param1 = cprotocols;
            document.write('<iframe src="' + param1 + '"></iframe>');
        }
        </script>
    </p>

That's what i have in the view right now, but what ends up happening is that:

  1. The frame appears AFTER I press submit; ideally, I'd like the frame to already exist on the page, then have the url requested load inside it after submit is clicked. Is this possible?

  2. A new page pops up, the frame is there, but I get a 404 "resource does not exist" error (obviously from the javascript syntax being wrong). My goal is to have the file requested load on the same screen as the DDL.

  3. The URL is dealt with entirely by the Handle() action.


You're better off just putting it in your page as path of the existing HTML (perhaps in your master page), then use JavaScript to set the SRC when the selection takes place.

<iframe id="frm1" src="about:blank"></iframe>

js:

document.getElemenyById('frm1').src="...."
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜