Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
jQuery script 'works' but its very cowboy - how can i make it better?
Old 11-13-2009, 03:03 AM jQuery script 'works' but its very cowboy - how can i make it better?
TWD
TWD's Avatar
King Spam Talker

Posts: 1,112
Trades: 0
Code:
//gets all image links with ref to the "product_images" directory
  
          $j("a[href*='product_images/']").each(function() {
        
               //gets the existing href attribute        
         
               var linkhref = $j(this).attr("href");

               //writes the new link reference to the anchor link element
     
              $j(this).attr("href", linkhref.replace(".png", "a.png"));
          
               newfile = $j(this).attr("href").split('/').pop();
        
              $j(this).attr("href", ("http://www.mywebsite/wp-content/uploads/" + newfile));
     
         });//ends "each" function
I am using $j to call jQuery in no-conflict mode.

Basically I want to just rename some links to a different directory.

I am sure there are much cleaner ways to write this.

For a start, it assumes that the files will always be in .PNG format. But if someone uses a GIF the script breaks.
How to fix that?

Second, is there a more portable way of renaming the new path rather than manually typing the complete path as I have done here?

Last edited by TWD; 11-13-2009 at 07:52 PM..
TWD is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-13-2009, 07:52 PM Re: jQuery script 'works' but its very cowboy - how can i make it better?
TWD
TWD's Avatar
King Spam Talker

Posts: 1,112
Trades: 0
Perhaps to make it clearer,

the new file has the same file name with the suffix "a" added.
Example

myfile.png

rename to

myfilea.png


The existing file is in the "product_images" directory.
I want to rename it to the "uploads" directory.

Last edited by TWD; 11-13-2009 at 07:53 PM..
TWD is offline
Reply With Quote
View Public Profile
 
Old 11-14-2009, 12:22 PM Re: jQuery script 'works' but its very cowboy - how can i make it better?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Just split the href on the '.' and use the last member of the array to determine what the existing suffix is.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to jQuery script 'works' but its very cowboy - how can i make it better?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23136 seconds with 12 queries