0

Okay, so here is my code:

//Other Jquery codes


// show_popup_crop : show the crop popup
function show_popup_crop(url) {
    alert("Called show_popup_crop!");
    // change the photo source
    $('#cropbox').attr('src', url);
    // destroy the Jcrop object to create a new one
    try {
        jcrop_api.destroy();
    } catch (e) {
        // object not defined
    }
    // Initialize the Jcrop using the TARGET_W and TARGET_H that initialized before
    $('#cropbox').Jcrop({
        aspectRatio: 1,
        setSelect: [ 100, 100, TARGET_W, TARGET_H ],
        onSelect: updateCoords
    },function(){
        jcrop_api = this;
    });

    // store the current uploaded photo url in a hidden input to use it later
    $('#photo_url').val(url);
    // hide and reset the upload popup
    $('#display_pic_input_first').val('');

    // show the crop popup
    $('#popup_crop').show();
}

// crop_photo :  
function crop_photo() {
    var x_ = $('#x').val();
    var y_ = $('#y').val();
    var w_ = $('#w').val();
    var h_ = $('#h').val();
    var photo_url_ = $('#photo_url').val();

    // hide thecrop  popup
    $('#popup_crop').hide();

    // display the loading texte
    $('.loading').css('display','inherit');
    // crop photo with a php file using ajax call
    $.ajax({
        url: 'crop_photo.php',
        type: 'POST',
        data: {x:x_, y:y_, w:w_, h:h_, photo_url:photo_url_, targ_w:TARGET_W, targ_h:TARGET_H},
        success: function(data){
            // display the croped photo
        }
    });
}

// updateCoords : updates hidden input values after every crop selection
function updateCoords(c) {
    $('#x').val(c.x);
    $('#y').val(c.y);
    $('#w').val(c.w);
    $('#h').val(c.h);
}

//document.ready function calls

What is happening:

  • The function show_popup_crop(url){} ain't working.
  • Now the function is being called, because the alert("Called show _popup_crop!"); is being executed. But it isn't executing the rest of the code that is the part where it needs to change the attributes of the image tag and the divisions.
  • I have tried changing the position of this script in the JS file but it doesn't help.


A PHP code automatically generates a script which is included in the body:

<script type="text/javascript">window.top.window.show_popup_crop("some url")</script>

PHP code within the script tag in the php page:

echo '<script type="text/javascript">window.top.window.show_popup_crop("'.$target_file.'")</script>';


  • Now it does call the function when the page is loaded as the alert function
    alert("Called show_popup_crop!"); is executed. In fact it executes all the alert functions included but it doesn't execute anything else included in the method.

I have tried removing everything else and just executing the code below, but it still doesn't work:

function show_popup_crop(url) {
    alert("Called show_popup_crop!");
    $('#cropbox').attr('src', url); //change the photo source
    $('#popup_crop').show();
}
  • However when I included $('#cropbox').attr('src', "some url"); in another document.ready method in the same JS file, it does execute the code. I don't understand what is the problem.
  • All the other functions that are called in index.php page are executed.

  • However one more weird thing is, it doesn't execute all the functions in $(document).ready(function(){...});.
    It just executes the first function and stops... but it executes all the other functions in all the other pages?


Console Error:

At first it gave me this console error:

Uncaught TypeError: undefined is not a function

but now it is giving me this:

Uncaught ReferenceError: jQuery is not defined

Files included in header (where position.js is the name of the file):

<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="js/jquery.Jcrop.min.js"></script>
<script src="js/position.js"></script>


P.S: Often when chrome is refreshed and the js document is changed, the chrome loads old js document no matter how many times it is refreshed?

poke321
  • 3
  • 5
  • **To fix your caching issue**: **(1)** Open DevTools by pressing F12. **(2)** Click on *Settings* (the little gear in the top-right corner). **(3)** Under general, the first option is *"Disable cache (while DevTools is open)"*. Check that option, and then, while keeping DevTools open, refresh the page. – myfunkyside Nov 12 '14 at 09:32
  • This: `` can't be right. The PHP-dots and quotes should not be there: `("'.$target_file.'")` should be `("somefile.jpg")`... Please check to make sure this is the case. – myfunkyside Nov 12 '14 at 10:03
  • Also, should `'Jcrop'` in `` really start with a capital letter? Usually those file names are all lower case.. – myfunkyside Nov 12 '14 at 10:46
  • The rest of the functions aren't executed because they are never invoked (at least not in the script you supplied). These are all standard functions: `function nameOfFunction(){}`, meaning they have to be invoked somewhere in your code, like this: `nameOfFunction();`. For the first function, this happens in the script that is added by PHP, but for the other function this never happens... You can do this for instance by calling the function right after it's created, like this: `function crop_photo(){...} crop_photo();`. Whether that's the right time to invoke them, I don't know.. it's your code – myfunkyside Nov 12 '14 at 10:59
  • Well, yes the coding part is right because I have copied this code from another tutorial and it works over there. And $target_file is a variable...and I know it works because again I have copied this code from a tutorial and yes it is working over there. – poke321 Nov 12 '14 at 11:28
  • Yes, in the PHP script it is a variable. But as soon as the PHP writes that code to the page as JS script, that variable should become a string value, and the dots and quotes should be gone.. But hey, when you know, you know;) I'll leave you to it then – myfunkyside Nov 12 '14 at 11:49
  • Oh yeah right, it is a copy paste mistake! I copied it directly from the php echo and pasted it over here. You are right, I thought you were taking about the php echo part...yes I will edit the question and yeah it is executed as when it is executed. Any other issue that could cause the problem? @myfunkyside – poke321 Nov 12 '14 at 12:03
  • Try putting `console.log(url);` at the start of `show_popup_crop(url){}` and check it's value. *`console.log()` is a better alternative for `alert()` since it doesn't stop the execution of the code (like alert() does). Open the browser's DevTools (F12), and then click on the button "Console". Now you can see the console.logs appearing at the bottom of your screen as the code is executed.* – myfunkyside Nov 12 '14 at 12:23
  • I couldn't tell you if the Jcrop parts are correct, I have no experience with that plugin – myfunkyside Nov 12 '14 at 12:29
  • I did console.log(url)...it loads the correct data.... For Jcrop part, i tried removing the Jcrop part and just execute function show_popup_crop(url) { alert("Called show_popup_crop!"); $('#cropbox').attr('src', url); //change the photo source $('#popup_crop').show(); } But still not executing the part where it changes the attributes of the img and division. @myfunkyside – poke321 Nov 12 '14 at 12:41
  • Oh right, you said that in your question. Ehm.. Is your site online? Could you post the link so I could have a look at what exactly is happening? – myfunkyside Nov 12 '14 at 12:51
  • No its not live...actually the host am using is a free one and it doesn't have the updated version of php so a lot of my php functions dont work so haven't uploaded it...is there some other way you could take a look in this 'cause i have been trying to solve the same issue from last few days and I don't have enough reputation to call you to chat. @myfunkyside – poke321 Nov 12 '14 at 12:54
  • Mmm.. Oke **(1)** What is the value of `url`? (How are you running your PHP scripts offline btw?) **(2)** What (using DevTools' Element Inspector) is the value of `#cropbox`'s `src` tag? **(3)** Could you post the HTML code for the `#cropbox` element, and any other elements related to the JS code in your question. **(4)** Try changing `window.top.window.show_popup_crop("'.$target_file.'")` to just `show_popup_crop("'.$target_file.'")` (although that probably won't do much, since the function is already being executed). – myfunkyside Nov 12 '14 at 13:08
  • **(5)** Change your PHP code to this: `echo '';` *(And maybe without the `window.top.window.`, because that's probably not necessary anyway and I don't think it's widely supported yet. But if it works with, and you like that better, you can leave it in.)* – myfunkyside Nov 12 '14 at 13:16
  • 1. $target_file = $target_dir.$username_db."_temp.".$imageFileType; where $target_dir is the directory is profile_images/ and $username_db is the username of the user. So for the user myfunkyside, the generated url will be "profile_images/myfunkyside_temp.jpg". (I am using wampserver to run php scripts) – poke321 Nov 12 '14 at 13:17
  • 2. Initially the src value is nothing, but after the function loads, the tutorial where I copied the code from, over there src is replaced by the url. For me, the code doesn't work. This is where I took the code from [link](http://www.bewebdeveloper.com/tutorial-about-crop-photo-using-php-and-jquery) 3. Tried that too...but no luck. PS: Is there some way you could have a look to my code like by usinh TeamvieweR? – poke321 Nov 12 '14 at 13:18
  • 1. Okay, but I mean, what is the actual value on the page? What value did the `console.log(url)` give? – myfunkyside Nov 12 '14 at 13:22
  • 3. Sure, if that's something I can look at without having to create an account. Otherwise, if you have dropbox, you can upload your files to there and post the public link to each file. Or just upload the files to your server and post the direct links so I can download them. – myfunkyside Nov 12 '14 at 13:25
  • 1. profile_images/ashiskumarsen_temp.jpg And I have an error Uncaught TypeError: undefined is not a function position.js:28show_popup_crop position.js:28(anonymous function) – poke321 Nov 12 '14 at 13:25
  • @myfunkyside Hey thank you soooo much ! Looks like **echo '';** did the trick !! :) :) :) It is working ! :) You should post this as an answer ! :) – poke321 Nov 12 '14 at 13:33
  • I posted it as an answer with explanation. Glad I could finally fix this for you, I should have thought of this solution immediately since images were involved.. – myfunkyside Nov 12 '14 at 14:04
  • @myfunkyside I finally feel relived ! I marked your answer as accepted one :) Thanx for the help :) – poke321 Nov 12 '14 at 14:36

2 Answers2

1

Change your PHP code to this:

echo '<script type="text/javascript">$(window).on("load",function(){window.top.window.show_popup_crop("'.$target_file.'");});</script>';

The result in JavaScript will be this:

<script type="text/javascript">
    $(window).on("load",function(){
        window.top.window.show_popup_crop("url/to/image.jpg");
    });
</script>
  • What was happening in your code, was that show_popup_crop() was being called before the image-element #cropbox had been fully loaded on the page.
  • Because of that, this line $('#cropbox').attr('src', url); tried to change the src tag of an element that didn't exist yet.
    And by the time the image-element had finally been loaded, the code had tried to execute that line long ago (in computer-time, this all happens within a second or less) without any success.
    So nothing happened.

What the $(window).on("load",function(){...}); does, is wait until all elements on the page are fully loaded, before executing the code within it's handler-function.
This happens after document.ready. Read here what the difference between the two is, and why it's important to use window.load especially for images (I always recommend it over document.ready).

So now, this line $('#cropbox').attr('src', url); is executed only after all elements - including the image - have been loaded on the page. So the image-element exists, and the source can be successfully changed.

Community
  • 1
  • 1
myfunkyside
  • 3,890
  • 1
  • 17
  • 32
  • Thank you so much for noting about the window.load functionality. Without it, jQuery was not executing any of it's prototype functions. – Marty McGee Nov 20 '20 at 15:08
0

did you wrap all your code around the

$(document).ready(function(){
  //jquery code here
});

I kind of have the feeling your problem is related to this. .ready is an event thrown by jquery when its API has loaded and is ready for use

Max Bumaye
  • 1,017
  • 10
  • 17
  • try to define your function like this: var showPopup = function(url){}; and try it like this – Max Bumaye Nov 12 '14 at 11:38
  • I changed the function as u showed..and wrapped document.ready around but not running. The code where I copied this from doesn't seem to have any issue at all? – poke321 Nov 12 '14 at 11:59