如果存在无法显示的.doc文件,则在iframe中显示消息

时间:2015-04-23 07:39:50

标签: javascript html iframe

我正在使用iframe在页面上显示pdf或doc文件。 pdf毫无疑问地显示完美,但doc文件刚刚下载,我知道原因。

现在问题是我想在iframe中显示该文件是doc文件的消息,因此无法显示,需要下载才能查看。

到目前为止,我已经搜索并找到了各种解决方案,以便在加载iframe时显示消息或任何内容,但我想这不是加载任何内容的情况。

这里是fiddle

3 个答案:

答案 0 :(得分:0)

显示一些示例代码,以便我们为您提供最佳解决方案。你也可以使用js小提琴。

否则,您可以添加if条件以检查检查文件类型并向用户显示警报通知。

答案 1 :(得分:0)

在更改或设置iframe之前,请检查文件类型是什么?

如果是pdf,则在iframe中显示该文件,如果是doc,则使用该消息加载另一个html文件。

另外,正如另一个人所说,请展示一些你做过的例子!

编辑:使用javascript。看看这段代码并告诉我它是否有帮助 http://jsfiddle.net/7803gakc/1/

// Not really sure about another way if it's going to be pure js/html and nothing server-side

// Here's the location to the file
var file = "http://www.ieee.org/documents/ieeecopyrightform.doc",

// The frame being used
    frame = document.getElementById("frame"),

// Files that will be allowed to be embedded based on extension
    extensions = ["pdf"],

// This will tell us if it's right or not
    correct = false;

// Loop through the extension list
for(var i = 0; i < extensions.length; i++){
    // Check if the file url ends with the given extension
    if(file.substring(file.lastIndexOf(".")+1) == extensions[i]){
        // All conditions met, set to true!
        correct = true;
    }
}

if(correct){
    // Yay, it's correct!
    frame.src = file;
}else{
    // It's wrong, show something else!
    frame.src = "http://example.com";
}

答案 2 :(得分:0)

我无法找到任何在iframe中显示消息的解决方案,可能是它没有提供,所以我切换到了对象标签。

<object data="[path to .doc]" type="application/msword">
<p>It appears you do not have a Suitable plugin for this browser. You can download this file.
    <a href="[path to .doc]">
    <button type="button">download</button></a>
</p>