Oddly enough, works in IE but not FF or Chrome

Hi, everyone,

I’ve got a project that I work on in between official projects… kind of a “goof around and learn how it’s done” project.

I’m using jQuery to create links. All but one of them will change content in the main page. They all work flawlessly.

The one link, however, only works in IE8 and will not work in FF17 or Chrome26.

$('<span>Register</span>').css({float:'right', cursor:'pointer', padding:0, margin:'0px 15px'}).attr({'id':'reglink', 'class':'lnk'}).appendTo($header);

This places the word “Register” in the upper-right corner of my page.

$('#regLink').click(function(){$.get('testform.cfm',function(data){modal.open({content:data})}});

This makes it clickable and opens a page that contains a small form.

IE8 will do exactly as I want. FF and Chrome will not display the modal and will not throw an error. Any ideas?

V/r,

:slight_smile:

FF17 or Chrome26

Why are you using such old versions of these browsers? They update automatically, you should let them update automatically. FireFox is currently on 33 and Chrome is currently on 39.

The reason this isn’t working, is because the links are being added after the DOM has loaded. Use below instead of .click():

$(document).on('click', '#regLink', function() {
    $.get('testform.cfm', function(data){
       modal.open({content:data})
    });
});

This will register the listener to the document, rather than binding it to the element.

My dev system is isolated from the internet. My NIPR system doesn’t allow flash drives, nor do I have CD/DVD burn privileges. If we want a more recent version of FF or Chrome, we have to wait for our SA (the only person with CD/DVD burning permissions) to download it and put it to disk for us… when he has time. (EDIT: Although I suppose I could bring one from home, I guess.)

I’ll give the .on() a shot. But why is it working in IE?

UPDATE: Just tested your suggestion, and it isn’t working in FF or Chrome, either.

V/r,

:slight_smile:

Ug, I couldn’t live like that. I thought this was your home project.

I think I need to see more code then. What exactly is it doing? Is the click being recognized? Do this:

$(document).on('click', '#regLink', function() {
   console.log('Link Clicked'); 
   $.get('testform.cfm', function(data){
      console.log(data);
      modal.open({content:data});
    });
});

And tell me if you see anything in the console.

Nevermind… thanks for your help… I don’t know why, but every time I load the page it wouldn’t work… for no reason, I just clicked the reload button, and now it’s working… tried the exact same thing in Chrome… load the page, it wouldn’t work, refresh the page and suddenly it works. And the weird thing is that I always set my browsers to get fresh data on every page load, and clear the cache upon exit. So I don’t know why reloading/refreshing the page fixed it, but it did.

And we’ve discussed working for DoD, before. It’s not ideal as far as being able to get what I need/want to make my job easier, but I do work with some incredibly awesome people (both personality-wise and skill-wise) and can’t imagine myself back in the corporate environment.

Thanks, again.

V/r,

:slight_smile:

1 Like

So the $(document).on() got it? Good deal. Sometimes you have to refresh the file manually or use fingerprinting.

During development, you could try something like this:

<script src="/js/filename.js?v=#DateFormat(Now(), "ddHHmmssL")#"></script>

Just be sure to take it out before production. It’s a quick and dirty way to do file fingerprinting and force cache refresh. The whole fingerprinting idea exists because caching sucks and isn’t standardized. A true file fingerprint would be in the file name, which is usually done by automated builds.

filename-f3190d7bc457567f2aeb062a630bcd45.js

And we’ve discussed working for DoD…

Yeah. I just thought this was a home project. I’ve heard of people disabling auto-updates. One of the people I work with does it and it’s infuriating. lol

As far as fingerprinting, I’ve got an include that runs on every content load to check and make sure the document is being loaded within the mainContent area - if it isn’t it reloads the browser so that it is loading as I developed it to. If I run FireBug, every click does two things - loads the requested document, then runs that check.

GET http://127.0.0.1/zTESTINGz/home.cfm 200 OK 7ms
GET http://127.0.0.1/zTESTINGz/js/fscheck.js?_={random numbers - always changing}

… but that’s running after the page is loaded. I might be able to run it before the page is loaded… maybe… :smile:

CORRECTION: in all cases, the script include is the first line of code, so although FireBug shows it loading after the page loads, it’s actually executing before the page load. But since it’s a .js file, that will just make sure that the js is fresh, not the .cfm page. Sigh. :smile:

V/r,

:slight_smile:

1 Like

A follow up question.

Now that it’s working, the code that I have for the modal (HERE) I have modified to “get” a .cfm page that contains the form. I’m trying to get it so that after the form submits via AJaX (using preventDefault), a successful submit will then close the modal.

Now, code for closing the modal does exist for the parent page… but I can’t figure out how to get it so that the modal can close itself. I’ve tried “modal.method.close()”, “document.modal.method.close()”, “parent.modal.method.close()”, “parent.document.modal.method.close()”, and “top.window.modal.method.close()” to no avail. How can I get the .cfm page to reference the parent function for closing the modal?

Thanks,

:slight_smile:

From his code:

// Close the modal
method.close = function () {
	$modal.hide();
	$overlay.hide();
	$content.empty();
	$(window).unbind('resize.modal');
};

Then call method.close();. You can call that anywhere. So if you’re using jQuery:

$.post(url, formData, function(data) {
   if(data == "true") { // Whatever you're returning if successful
      method.close();
   } 
 });

I’ve tried “method.close()”, but I get the “method.close is undefined” message in FireBug.

V/r,

:slight_smile:

Sorry, I shouldn’t have said anywhere.

Can you post your code on jsfiddle or jsbin?

fiddle/bin blocked. If I had a CD formatted for PC, I could burn a disc and paste it in here. Getting a disc that won’t hard-freeze my dev system upon insert is not an easy task. I’ve updated the BIOS and the DVD drivers, but the issue is with a Windows update that is applied to new systems on the dev network - before the update, the DVDs work fine; but the update (which is network specific) will freeze my system so that even CTRL-ALT-DEL won’t free it.

I’ll see what I can do.

V/r,

:slight_smile:

Wow. That’s alot of trouble. How much is there? Can you just post it here?

I just mean the frontend. Not the CF.

I found a disc that didn’t lock my system… the JS alone (mostly what’s in the link, above) is 31 lines. I’ll remove the CF bits and post the form and the JS for that. As soon as it’s done copying.

// parent page

var modal = (function(){
    var method = {}, $overLay, $modal, $content, $closer;
    $overLay = $('<div id="overLay"></div>');
    $modal = $('<div id="modal"></div');
    $content = $('<div id="content"></div');
    $closer = $('<a href="javascript:void(0);" id="closer"></a>');
    $modal.hide(); $overLay.hide(); $modal.append($content,$closer);
    method.center = function(){
        var top, left;
        top = Math.max($(window).height() - $modal.outerHeight(),0) / 2;
        left = Math.max($(window).width() - $modal.outerWidth(),0) / 2;
        $modal.css({top:top + $(window).scrollTop(),left:left + $(window).scrollLeft(),zIndex:10000000});
        $overLay.css({zIndex:9999999});
        };
    method.open = function(settings){
        $content.empty().append(settings.content);
        $modal.css({width:settings.width || 'auto',height:settings.height || 'auto'});
        method.center();
        $(window).bind('resize.modal',method.center);
        $modal.show();
        $overLay.show();
        };
    method.close = function(){
        $modal.hide(); $overLay.hide(); $content.empty(); $(window).unbind('resize.modal');
        };
    $closer.click(function(e){
        e.preventDefault();
        method.close();
        }); $framst.append($overLay,$modal);
    return method;
    }());

$(document).on('click','#regLink',function(){$.get('testform.cfm',function(data){modal.open({content:data})});});

// testform.cfm

                <form id="thisForm" name="thisForm" style="width:500px;" method="POST">
                    <table id="formTable" name="formTable" style="width:500px;">
                        <tr>
                            <th colspan="2" style="text-align:center; font-style:italic;">Account Registration Form</th>
                        </tr>
                        <tr>
                            <td style="text-align:right; width:150px;"><span class="reqd">*</span>First Name:&nbsp;&nbsp;</td>
                            <td style="text-align:left;"><input name="firstName" type="text" id="firstName" style="width:250px;" maxlength="50"></td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><span class="reqd">*</span>Last Name:&nbsp;&nbsp;</td>
                            <td style="text-align:left;"><input type="text" id="lastName" name="lastName" style="width:250px;" maxlength="50" /></td>
                        </tr>
                        <tr>
                            <td style="text-align:right;">Organization:&nbsp;&nbsp;</td>
                            <td style="text-align:left;"><select id="userOrg" name="userOrg" style="width:256px;">
                                                            <option value="">Select</option>
    <!--  CF code for creating options pulled from database - removed for brevity -->
                                                         </select></td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><span class="reqd">*</span>Username:&nbsp;&nbsp;</td>
                            <td style="text-align:left;"><input type="text" id="userName" name="userName" style="width:250px;" maxlength="50" /></td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><span class="reqd">*</span>Password:&nbsp;&nbsp;</td>
                            <td style="text-align:left;"><input type="password" id="userPassword" name="userPassword" style="width:250px;" maxlength="50" /></td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><span class="reqd">*</span>Re-enter Password:&nbsp;&nbsp;</td>
                            <td style="text-align:left;"><input type="password" id="userPassword2" name="userPassword2" style="width:250px;" maxlength="50" /></td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align:center;"><input type="button" id="submitBtn" name="submitBtn" value="Register" style="width:90px;" onClick="return checkData(this.form);" />
                                        &nbsp;&nbsp;<input type="reset" id="resetBtn" name="resetBtn" value="Reset" style="width:90px;" /></td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align:right;"><span class="reqd">*</span>required</td>
                        </tr>
                    </table>
                </form>

                <script type="text/javascript">
    
                    function checkData(formObj){
                        var warn = "", focusOn = [], nameMask = /^[-\s\'A-Z]+$/gi, unameMask = /^[_\s\'A-Z]+$/gi, 
                            pwMask = /(?=(.*[0-9]){2,})(?=(.*[A-Z]){2,})(?=(.*[a-z]){2,})(?=(.*[\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+]){2,}).{8,}/;
                        formObj.firstName.value = $.trim(formObj.firstName.value);
                        formObj.lastName.value = $.trim(formObj.lastName.value);
                        formObj.userName.value = $.trim(formObj.userName.value);
                        formObj.userPassword.value = $.trim(formObj.userPassword.value);
                        formObj.userPassword2.value = $.trim(formObj.userPassword2.value);
                        var firstName = formObj.firstName.value,
                        lastName = formObj.lastName.value,
                        userName = formObj.userName.value,
                        userPassword = formObj.userPassword.value,
                        userPassword2 = formObj.userPassword2.value;

            // form validation - removed for brevity

                        switch(warn.length){
                            case 0:
                                $.post('testform.cfm',$('#thisForm').serialize())
                                    .done(function(data){
                                        alert(data);
                                        top.window.modal.method.close();
                                        });
                            break;
                            default:
                                alert(warn); return false;
                            break;
                            }
                        }
    
                </script>

This works for me. I had to change $framst to $('body'). You also weren’t giving it any text for the close button.

Sorry, I didn’t look at his code too closely earlier.

You’re creating a modal object with an alias of method inside of it, which you’re returning. method is not directly accessible, it’s returned to the main object. So anything would be accessed with the object name, which is modal. So, modal.close() or modal.open({content:"stuff"});.

See my comments. It’s ugly but should be somewhat readable.

var modal = (function(){
    var method = {}, $overLay, $modal, $content, $closer;
    $overLay = $('<div id="overLay"></div>');  // His CSS uses overlay not overLay if you're copy and pasting that
    $modal = $('<div id="modal"></div');
    $content = $('<div id="content"></div');
    $closer = $('<a href="javascript:void(0);" id="closer">X</a'); // You forgot to add an X here. Still not showing up right, but I'll let you fix that.
    $modal.hide(); 
    $overLay.hide(); 
    $modal.append($content,$closer);
    
    method.center = function(){
        var top, left;
        top = Math.max($(window).height() - $modal.outerHeight(),0) / 2;
        left = Math.max($(window).width() - $modal.outerWidth(),0) / 2;
        $modal.css({top:top + $(window).scrollTop(),left:left + $(window).scrollLeft(),zIndex:10000000});
        $overLay.css({zIndex:9999999});
        };
    
    method.open = function(settings){
        $content.empty().append(settings.content);
        $modal.css({width:settings.width || 'auto',height:settings.height || 'auto'});
        method.center();
        $(window).bind('resize.modal',method.center);
        $modal.show();
        $overLay.show();
        };
    
    method.close = function(){
        $modal.hide(); $overLay.hide(); $content.empty(); $(window).unbind('resize.modal');
        };
    
    $closer.click(function(e){
        e.preventDefault();
        method.close();
     }); 
    
    $('body').append($overLay,$modal); // You never defined $framst in the object. If it's defined outside, it's losing scope.
    
    return method;
}());

$(document).on('click','#regLink',function(){
    modal.open({content:"test"}); // call the modal object itself
});

$framst is being declared at the very top of $(document).ready(), and the closer <a> is being given a small image via JS. Didn’t think to add my CSS (it was a long day.)

From the parent, I can use modal.close(); and it works. From within testform.cfm, it’s undefined. Shouldn’t parent.modal.close() refer to it?

V/r,

:slight_smile:

parent is for frames. Are you using frames?

I don’t use frames so I’m no help there. I avoid them like the plague. (as should you! lol :smiley:)

Frames - only if the client holds a family member hostage.

iFrames, on the other hand, I’m fond of.

On the one hand, yes, the modal is in the same page; but it’s getting content from another page, which I assume to be in a separate scope.

V/r,

:slight_smile:

No, if you’re doing it with AJAX you’re loading it into your current DOM.

iFrames, on the other hand, I’m fond of.

I avoid all of them. lol