Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2012, 03:54 AM   PM User | #1
moonpeep
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
moonpeep is an unknown quantity at this point
onclick function

Trying to learn how to create functions... This works:
Code:
<script>
$(document).ready(function() {
var $sigDiv = $("#signature1").jSignature({'UndoButton':false,color:"#000000",lineWidth:2});
var datapair = $sigDiv.jSignature("getData", "image");
        var i = new Image();
        i.src = "data:" + datapair[0] + "," + datapair[1];
        $(i).appendTo($("#imag"));
});
</script>

<div id="signature1"></div>
<div id="imag" style="height:340px;"></div>
BUT, I want the last 4 lines of the script to happen onclick, so I tried:

Code:
<script>
$(document).ready(function() {
var $sigDiv = $("#signature1").jSignature({'UndoButton':false,color:"#000000",lineWidth:2});
});
</script>

<script>
function placesig() {
var datapair = $sigDiv.jSignature("getData", "image");
        var i = new Image();
        i.src = "data:" + datapair[0] + "," + datapair[1];
        $(i).appendTo($("#imag"));
};
</script>

<div id="signature1"></div>
<div id="imag" style="height:340px;"></div>

<input type="button" value="Place Image" onclick="placesig();"/>
This does not work. I've attempted to find the solution, but my lack of js understanding is making it difficult. Any help would be much appreciated. Thanks!
moonpeep is offline   Reply With Quote
Old 12-15-2012, 04:07 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,207
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
First of all, you are only using vanilla JavaScript to a very small degree.

Most of what you are doing is using the jQuery library. SO you may need to ask this in the jQuery forum, not the vanilla JavaSript forum.

BUT...

This part of the code is pure JavaScript, and it makes NO SENSE AT ALL:
Code:
        var i = new Image();
        i.src = "data:" + datapair[0] + "," + datapair[1];
You create an Image object. Good. But then you try to make the .src of the object--which, for an Image object, means the FILENAME OF THE IMAGE--be some funky date: xxx,yyy string.

How do you expect the Image to then show up on the page if you don't give it a valid FILE NAME (techinically, a file URL, but if you use only a name it is assumed to be in the same domain and is converted to a URL).

Then, to make matters even stranger, you appear to be appending your newly created Image object to the Image object that already exists on the page with the ID of "img".

WEIRD.

You say the first version works. I have to believe you. But it makes no sense to me.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 12-15-2012, 04:17 AM   PM User | #3
moonpeep
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
moonpeep is an unknown quantity at this point
Hmm. Well the first does work - just checked. I did forget to include:
Code:
<script src="jSignature.js"></script>
Which I assume is handling the file naming. The image ends up being download.png.

Since it happens on document ready in the first version, the canvas and resulting image is blank, except for a line - but it is a real png.

I just need to call the same function after some data has been put into the canvas.

Thanks for your reply!
moonpeep is offline   Reply With Quote
Old 12-15-2012, 04:49 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,207
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
So have you used a debugger and traced through what it happening??

I'd be curious to know the value of some things at certain points in that code (original code fine).

Code:
var datapair = $sigDiv.jSignature("getData", "image");
        var i = new Image();
BREAKPOINT:  Find the values of datapair[0] and datapair[1] then STEP OVER and
        i.src = "data:" + datapair[0] + "," + datapair[1];
Find the value of i.src then STEP OVER
        $(i).appendTo($("#imag"));
Find what the <img id="imag"> now looks like.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 12-15-2012, 04:54 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,207
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Believe it or not, I *MAY* know why it doesn't work!

In *THIS* code:
Code:
$(document).ready(function() {
    var $sigDiv = $("#signature1").jSignature({'UndoButton':false,color:"#000000",lineWidth:2});
});
$sigDiv is a *LOCAL VARIABLE!*

As soon as that "ready" anonymous function is finished executing, that variable *GOES AWAY*! Poof! Vanished!

So the answer is simple: Make it a GLOBAL variable:
Code:
<script type="text/javascript">
var $sigDiv = null; // GLOBAL!

$(document).ready(
    function() 
    {
        // no var here!!!
        $sigDiv = $("#signature1").jSignature({'UndoButton':false,color:"#000000",lineWidth:2});
    }
);

function placesig() 
{
    var datapair = $sigDiv.jSignature("getData", "image");
    var i = new Image();
    i.src = "data:" + datapair[0] + "," + datapair[1];
    $(i).appendTo($("#imag"));
};
</script>
At least try it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
moonpeep (12-15-2012)
Old 12-15-2012, 05:08 AM   PM User | #6
moonpeep
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
moonpeep is an unknown quantity at this point
You're a genius! Worked like a charm Thank you!
moonpeep is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:39 PM.


Advertisement
Log in to turn off these ads.