1

I want to load a HTML file in an Iframe dynamically.

I tried the following code:

<script type="text/javascript">
$(document).ready(function () {
    $('a').click(function (e) {
        e.preventDefault();   
        filename = "\"+$(this).text() + ".htm";
        alert(filename);
        $('iframe').attr('src', filename);
    });
}); </script>  

Directory Structure: E:SVN_HobbyHomes\HobbyHomesWebApp\HTML\Dancing.htm

It doesn't load the file in the path inside the Iframe. Howevr if i write like this it works but i don know how to use @Url.Content in javascript

<iframe id="iframe" frameborder="0" src=src="@Url.Content("~/HTML/Dancing.htm")" style="width: 100%; height: 700px;"></iframe>

Stil it gives me error:Resource Could notbe Found!!!

0

2 Answers 2

3

The source of an iframe has to be a URL, either absolute or relative. Yours is not a URL, but a local filename.

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

4 Comments

seeing my path can u tell me how should iwrite so that it fetches file
You can access your file by using relative file notation, e.g. filename = "/"+ $(this).text()+".html";, if the $(this).text() points to a file in the same directory as the currently loaded file. Beware that your code allows for Cross-Site Scripting (XSS), if you do not sanitize the user input in $(this).text().
Hey i tried wat u suggested but i am gettin an error as Resource Not found.please help me
Update your first post with the code you're using right now. Also tell us how your directory structure is ordered, e.g. where the files you're trying to load are located.
1

This is most likely a browser security restriction. For (obvious) reasons, in-browser code isn't allowed to access local resources on the workstation (without plugins and whatnot which explicitly grant more permissions).

If a web page could access a file on somebody's local workstation, what else could it do? What other files would you be able to arbitrarily access? The security implications are pretty clear.

You need to serve the file the same way you serve the web page trying to access it.

2 Comments

So how can i access my dat page..Please help me
Looking at the updated question, it sounds like you're confusing server-side paths with client-side paths. If the page exists on the server and is visible by a client then you'll need to provide to the client a client-visible path. This is usually in the form of a relative URL or an absolute URL from the web server root. The client won't be able to see server drive letters and things like that. So something like @Url.Content("~/HTML/Dancing.htm") would translate the server-side path you're giving it to a client-side path. Look at the rendered HTML to see that path.

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.