简体   繁体   English

在javascript中设置默认图像

[英]Set default image in javascript

I have a requirement where i need to set a default image if the image is not uploaded by user. 我有一个要求,如果图像没有被用户上传,我需要设置默认图像。 I am trying to set the image using javascript. 我正在尝试使用javascript设置图像。 My image is located in images folder of my project in eclipse. 我的图像位于eclipse中我项目的图像文件夹中。

my html code is: 我的HTML代码是:

<tr>
    <td align="right">Upload Image :</td>
    <td>
         <input type="file"  id="imageNonSaml" name="imageNonSaml" value=""/>
    </td>
</tr>

my javascript is : 我的javascript是:

function testSSO(){
    var image=document.getElementById("imageNonSaml").value;
    if(image==null || image==''){
        alert("inside if");
        document.getElementById('imageNonSaml').setAttribute('name',"/assets/img/generic.png");
    }
}

You're trying to change the name attribute of the file input. 您正在尝试更改文件输入的name属性。 It's not possible to set the path of the a file input. 无法设置文件输入的路径。 The reason for this is security - if it was possible, a malicuous site could set the value and automatically submit the form in order to steal files from the user's system. 这样做的原因是安全性 - 如果可能的话,一个恶意网站可以设置值并自动提交表单以便从用户的系统中窃取文件。

If you're intending to set the src of an <img /> then you need to reference it in your getElementById() call, and set the src property, not the name attribute. 如果您打算设置<img />src ,则需要在getElementById()调用中引用它,并设置src属性,而不是name属性。

document.getElementById('myimg').src = "/assets/img/generic.png";

You can't modify the FILE typed input with JavaScript. 您无法使用JavaScript修改FILE类型的输入。 This would be a major security issue, if sites could automatically upload files from your machine. 如果站点可以自动从您的计算机上传文件,这将是一个主要的安全问题。

try this. 尝试这个。

function testSSO(){
 var image = document.getElementById("imageNonSaml");
 if(image.value == null || image.value == ''){
  alert("inside if");
  image.value = "/assets/img/generic.png";
 }
}

It is possible to set a default image, but not with the File element using JavaScript. 可以使用JavaScript设置默认图像,但不能使用File元素。 Basically you have 3 options: 基本上你有3个选择:

1.) Set it as a default value in your database. 1.)将其设置为数据库中的默认值。 Thus if no change is made, a default image is loaded when requested from the database. 因此,如果不进行任何更改,则在从数据库请求时加载默认图像。

2.) Set the image in your server side language. 2.)以服务器端语言设置图像。 Thus if no image is in the database, a default image is shown. 因此,如果数据库中没有图像,则显示默认图像。 <img src="<% echo image %>" />

3.) Set the Img element src attribute in JavaScript to the default image if no image can be loaded. 3.)如果无法加载图像,请将JavaScript中的Img元素src属性设置为默认图像。

With jQuery this would look similar to below: 使用jQuery,这看起来类似于下面:

$("#imageid").attr("src","mydomain.com/img/imgname.png");

Or without jQuery see jay harris answer. 或者没有jQuery看到杰伊哈里斯的回答。

As to the best option, it depends on your requirements. 至于最佳选择,它取决于您的要求。 Number 3 could be problematic if someone had JavaScript disabled (not likely nowadays) but still could happen. 如果有人禁用JavaScript(现在不太可能),但仍有可能发生,那么3号可能会有问题。 I would consider 1 or 2 based upon the exact requirements of my project. 我会根据项目的具体要求考虑1或2。

EDIT: 编辑:

To set your path in JavaScript I would use the full domain like so: 要在JavaScript中设置路径,我会使用完整的域,如下所示:

//get the url based upon what is in the browser
var url = location.protocol + "//" + location.host;
//build the image url from this
var imgurl = url + "/assets/img/generic.png";
//plug it into the jQuery selector
$("#imageid").attr("src",imgurl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM