CourseSites jQuery plug-in

11
CourseSites jQuery plug-in

Transcript of CourseSites jQuery plug-in

Page 1: CourseSites jQuery plug-in

CourseSites jQuery plug-in

Page 2: CourseSites jQuery plug-in

Context

• Replace specific string(s) on a page that is part of href with something else

Page 3: CourseSites jQuery plug-in

CourseSites

• Instructor homepage (ie. https://wayank12teacher2.coursesites.com)– List of courses• Course101 (https://

wayank12teacher2.coursesites.com/s/_course101)• Course102

(https://wayank12teacher2.coursesites.com/s/_course102)

• Goal replace wayank12teacher2 with www

Page 4: CourseSites jQuery plug-in

Firebug…locate the element

Page 5: CourseSites jQuery plug-in

Firebug…locate the element (2)

Page 6: CourseSites jQuery plug-in

Element of interest

• <li class="course-navigation-course"><a class="highlight" href="https://wayank12teacher2.coursesites-stage.com/s/_wayank12changestructure101">wayank12changestructure101»</a></li>

Page 7: CourseSites jQuery plug-in

Find those elements

• jQuery('li.course-navigation-course').find('a[href]')

• Results:• [<a class= "highlight" href= "https: / /

wayank12teacher2.coursesites-stage.com/ s/ _wayank12changestructure101"> wayank12changestructure101» </a>

• , <a class= "highlight" href= "https: / / wayank12teacher2.coursesites-stage.com/ s/ _wayank12nov04test101"> wayank12nov04test101» </a>

• ]

Page 8: CourseSites jQuery plug-in

Search and Replace ( Use Firebug if you have to)

• location.host• Result "wayank12teacher2.coursesites-

stage.com"

• var b='https://' + location.host.replace(location.host.split('.')[0],'www')

Page 9: CourseSites jQuery plug-in

The plug-in

• jQuery.fn.convertUsertoWWW = function() { return this.each( function() { – var c=$(this).attr('href'); – var b='https://' +

location.host.replace(location.host.split('.')[0],'www')+c;

– $(this).attr('href',b); – }

• );

Page 10: CourseSites jQuery plug-in

To execute

• jQuery('li.course-navigation-course').find('a[href]').convertUsertoWWW();

Page 11: CourseSites jQuery plug-in

Final script

• jQuery(document).ready(function() { jQuery.fn.convertUsertoWWW = function() { return this.each( function() { – var c=$(this).attr('href'); – var b='https://' + location.host.replace(location.host.split('.')

[0],'www')+c; – $(this).attr('href',b); } );

• };

• jQuery('li.course-navigation-course').find('a[href]').convertUsertoWWW();