PDA

View Full Version : can a variable be used to store image



meenakshi
07-02-2008, 07:08 PM
hi
i m not sure but wanted to use a variable to store an image and then show it inside the table
something like


var img1;
img1="c:\myself.jpg";

and inside the table it shows like
<table>
<td>img1</td>
</table>

and this way it shows the image
is there a way to do such a thing
pls advice:)
smile always
anand

jscheuer1
07-02-2008, 07:34 PM
You can display images wherever you like, but you must involve the image tag at some point. This would work:


<script type="text/javascript">
var img1;
img1="c:\myself.jpg";
</script>

<table>
<td><script type="text/javascript">
document.write('<img src="'+img1+'">');
</script></td>
</table>

Also, with server side language you can do it more like what you have, but still not so simply. However, this is the javascript forum, so I have given you a javascript answer. There are other ways, even in javascript, but they all involve some tag (usually an image tag, the object tag can be used in some browsers with some image types) to display the image.

meenakshi
07-03-2008, 02:44 AM
sir,
thanks for your help
can you suggest me some other way to make it function because i m not able to display the pic
i m posting the code for reference
the td area marked as red is where i want to put the image
it wud be really kind of you if you can help me in this regard:)



function getSearchResults()
{try
{
var para = document.getElementById("txtSearchPara").value;
var dbfile = getDBFile();
var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source ="+ dbfile+ ";Persist Security Info=False";
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "select * from test where " +
"clid like '%" + para + "%' or " +
"tel2 like '%" + para + "%' order by clid desc";

rs = cn.Execute(SQL);
var resultString = "<table>";
resultString = resultString + "<tr> " +
"<td></td>" +
"<td></td>" +
"<td></td>" +
"</tr>" ;

var a1;
var a2=0;
var color;
var bcolor;
while (!rs.EOF )
{
color="#348781"; // green colour
bcolor="white";


resultString = resultString + "<tr> " +
"</tr>" +
"</table>" +
"<table>" +
"<tr>" +
"<td>" +"Stitching Number-"+ rs("clid")+"</td>"+
"<td>" + "Trial Date-" + rs("tdat")+ "</td>" +
"<td>" + "Delivery Date-" +rs("ddat")+ "</td>"+
"</tr>";
rs.MoveNext();
}
resultString = resultString + "</table>";
rs.Close();
cn.Close();
document.getElementById("SearchResultPanel").innerHTML=resultString;
}
catch (e)
{
alert ("getSearchResults() : " + e);
}
}
</script>
</head>
<body onload= getSearchResults(); >

<div>
<table >
<tr>
<td>
<input type="text" name="txtSearchPara" tabindex="19" onblur = "getSearchResults();">
</td>
</tr>
<tr>
<td><SPAN id= "SearchResultPanel" style = "position: relative; width: 100%; "></SPAN>
</td>

when i try to use ur code inside that td box it does not show any thing on page .must be some other way to do this thing
pls help
smile always:)
anand

thetestingsite
07-03-2008, 02:53 AM
var resultString = "<table>";
resultString = resultString + "<tr> " +
"<td></td>" +
"<td><img src='"+img1+"'></td>" +
"<td></td>" +
"</tr>" ;


Just keep in mind that you need to assign the variable img1 before this will actually work.
Hope this helps.

Nile
07-03-2008, 03:21 AM
TheTestingSite: Whats the difference between your code and this code?:


var resultString = "<table>";
resultString = resultString + "<tr>
<td></td>
<td><img src='"+img1+"'></td>
<td></td>
</tr>";

jscheuer1
07-03-2008, 03:48 AM
TheTestingSite: Whats the difference between your code and this code?:


var resultString = "<table>";
resultString = resultString + "<tr>
<td></td>
<td><img src='"+img1+"'></td>
<td></td>
</tr>";

In the highlighted section of your version the line ends without a terminating quote or a continue signifier. So, in all likelihood the script parser will barf on it. This is different than using 'echo' in PHP where the line break at the end of the line is seen as part of the string value. In javascript, lines that do not end in ; are still considered to end by most script parsers. Some browsers are better at guessing which lines should end, and which should not in javascript. But by the very nature (informal) of the language, it is impossible for any parser to guess right all of the time.

That's why (in javascript) it is best to actually use the ; for line ends, even though it isn't always required, and why termination with a concatenation signifier is required for most lines that 'continue below'.

Nile
07-03-2008, 03:55 AM
I see, so if you put all the script on the same line it'd execute fine in all browsers?

jscheuer1
07-03-2008, 04:15 AM
I see, so if you put all the script on the same line it'd execute fine in all browsers?

Which script? Um, it doesn't matter though. If you do that and haven't properly terminated your statements with semicolons, that will give rise to problems. But scripts can be all one line, if written properly.

Nile
07-03-2008, 04:19 AM
Hmm, I see. Thanks for the info :).

techietim
07-03-2008, 12:03 PM
Yup, it sure can.

http://www.hedgerwow.com/360/dhtml/base64-image/demp.php

jscheuer1
07-03-2008, 12:14 PM
Yup, it sure can.

http://www.hedgerwow.com/360/dhtml/base64-image/demp.php

I'm not sure if I mentioned or not that server side code can do this, it can - as your link demonstrates. Oh yeah, I did mention it:


Also, with server side language you can do it more like what you have, but still not so simply. However, this is the javascript forum, so I have given you a javascript answer.

However, this is the javascript forum, so -

No it (javascript) cannot.

And, if you are using server side code to render images from variables, you don't even need to bother with javascript.

techietim
07-03-2008, 12:50 PM
I'm not sure if I mentioned or not that server side code can do this, it can - as your link demonstrates. Oh yeah, I did mention it:



However, this is the javascript forum, so -

No it (javascript) cannot.

And, if you are using server side code to render images from variables, you don't even need to bother with javascript.

You must not of read the whole link I posted. You do not need PHP to make it work, but it does help. You can drop PHP out of the whole thing and just get the encoded string and use that without PHP touching anything.

jscheuer1
07-03-2008, 02:43 PM
You must not of read the whole link I posted. You do not need PHP to make it work, but it does help. You can drop PHP out of the whole thing and just get the encoded string and use that without PHP touching anything.

You're right, I didn't read the entire thing, but after looking at it some more, it is unclear to me how one gets this Base64 encoded string without something on the server side. To do it all with javascript, that (getting the string without the server's help) would be required.

If it can do that, that's great! It still looks quite complicated, do you have a simple (or other) implementation? I mean, if it can all be done with javascript, there should be a script I (or anyone with basic javascript knowledge) can use without needing to know all the details. Something where you plug in your image file and out comes the data and visa versa.

techietim
07-03-2008, 03:07 PM
Hmm, after playing with it, it seems that only Gecko based browsers display the image. It's only IE that requires the helping hand of PHP. Sorry for the misunderstanding.

@meenakshi

You can either use this method for Gecko only without the use of PHP, or use PHP and get IE support as well.

jscheuer1
07-03-2008, 04:24 PM
You know, it might actually be able to be done, but where's the demo that just about anyone can use? Perhaps (as I have done in the past with other members here) we could collaborate. The script branching in IE would have to be different. It would probably need to use vml and mht as noted on the page you linked to originally in this thread.

Before we started, if we were to, we should define exactly what the script is to be able to do. Any thoughts?