SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript_tag replacing whitespaces w/ '%20'

    Hello,

    I have a form that tests if a title has been changed since the page was loaded and the page has been submitted. If the page title has been changed then a confirm box will ask if the user wants to have the title changed. The problem is if the person hits cancel, all white spaces in the title get replaced w/ '%20'.

    Here's the code I was using:

    Code:
    <%=javascript_tag( "function confirmSubmit(){ var page_title_before= '#{u @page.title}';
                                                  var page_title_on_submit = escape($('page_title').value);
                                                      if( page_title_before != page_title_on_submit ){
                                                          var agree=confirm('Changing the title of this page will break any links or bookmarks that refer to it.\\nPress \"OK\" to save with the new title, or \"Cancel\" to revert to the original title.');  
                                                     if(agree){ return true; }
                                                                                                     else{  alert( 'page_title_before-->>' + escape(page_title_before) ); $('page_title').value = page_title_before; return false; }
                                                      }
                                                  else{ return true; }}") %>

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    else{  alert( 'page_title_before-->>' + escape(page_title_before) ); $('page_title').value = page_title_before; return false; }
    I think that is your problem. I think escape(page_title_before) will replace the white spaces with their escaped HTML equivalents, and the next statement copies that altered name to the page title.

    However, a couple of points.

    I think you can use a prototype observer to do what you want more neatly. There is a good section in Scott Raymond's book "Ajax on Rails" on this subject.

    My second point would be to wonder if your design could be improved by having it not rely on links that are broken by modification of a user accessible field such as the title. This is one of the reasons linking to individual records using the record id, is so useful.

    Lastly I'd recommend you have a look at the "Ajax on Rails" book. It is a very good starting point to help you more effectively use JavaScript within a Rails application. Writing raw JavaScript is not the best way forward.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •