Anyone here know how to use jQuery?

Talk about whatever topic you'd like, RPG related or not. (But please discuss things related to our software in the Tools section, below.)

Moderators: dorpond, trevor, Azhrei

Post Reply
User avatar
JonathanTheBlack
Dragon
Posts: 544
Joined: Mon Jun 28, 2010 12:12 pm
Location: I'm the worm...

Anyone here know how to use jQuery?

Post by JonathanTheBlack »

I would like to add a script to my web pages that replaces [this] with <a href="this.html">this</a>. I want to do this to cut down on HTML soup for long passages of text where I want a wiki like functionality on my page. Thanks!

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Anyone here know how to use jQuery?

Post by aliasmask »

You can use javascript. Basically you search the document for the key word. If you can narrow the search down to a div tag with a specific id it'll make the replace go smoother.

But JQuery does give you shortcuts if it's already installed. I found this line online doing a search for jquery find and replace:

Code: Select all

$('body').html($('body').html().replace('Original Text','New Text'));
It takes any html in the body tag and replaces and sets the text. But it indiscriminately replaces that key word no matter where it is and what context it's being used in. You can use '#id' instead of body to replace html only with tags with that id. But I think you have to add each on to it like this (id is the id value for the tag, so if your tag was myText it would be $('#myText').):

Code: Select all

$('#id').each(
   function () {
      this.html = this.html().replace('Original Text','New Text');
   });
It's been awhile for me, so if someone else can confirm this works. You don't need each if there is only one tag to be replaced on the page. If that's the case you can use the first example and just replace body with #id.

edit: Now, if you have a key word list that can be loaded on to the page, then instead of a static string, it could be dynamic and you can then loop to replace based on the key word. Also, your keywords should probably be encoded or changed to be url safe.

User avatar
JonathanTheBlack
Dragon
Posts: 544
Joined: Mon Jun 28, 2010 12:12 pm
Location: I'm the worm...

Re: Anyone here know how to use jQuery?

Post by JonathanTheBlack »

This is what I want to write in my .html file:

Code: Select all

<div id="content">
    <p>Adventuring [gear], [armor], [weapons], and [implements] are an important part of any adventurer's career. Their equipment is often all that stands between them and death. Each adventurer begins their career with a set of armor, a few weapons or implements, and a handful of coins in their pocket for adventuring goods. Characters start with one set of armor, up to three weapons or implements, and 100 silver to spend on adventuring gear. A shield may be chosen in place of a weapon or implement.</p>
</div>
This is what I want it to look like when someone loads the page:

Code: Select all

<div id="content">
    <p>Adventuring <a href="gear.html">gear</a>, <a href="armor.html">armor</a>, <a href="weapons.html">weapons</a>, and <a href="implements.html">implements</a> are an important part of any adventurer's career. Their equipment is often all that stands between them and death. Each adventurer begins their career with a set of armor, a few weapons or implements, and a handful of coins in their pocket for adventuring goods. Characters start with one set of armor, up to three weapons or implements, and 100 silver to spend on adventuring gear. A shield may be chosen in place of a weapon or implement.</p>
</div>
All I want to do is make the text parts of my web page more readable and less like HTML soup.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Anyone here know how to use jQuery?

Post by aliasmask »

Try this:

Code: Select all

var c = $("#content"); 
c.html( c.html().replace(/\[([^\]]*)\]/g,"<a href="$1.html">$1</a>") );

User avatar
JonathanTheBlack
Dragon
Posts: 544
Joined: Mon Jun 28, 2010 12:12 pm
Location: I'm the worm...

Re: Anyone here know how to use jQuery?

Post by JonathanTheBlack »

Um... where? :oops:

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Anyone here know how to use jQuery?

Post by aliasmask »

Put this at the top and modify the body tag.

Code: Select all

<html>
<head>
<script type="text/javascript">
   function load() {
      var c = $("#content"); 
      c.html( c.html().replace(/\[([^\]]*)\]/g,"<a href="$1.html">$1</a>") );
   }
</script>
</head>
<body onload="load()">...</body>
</html>

User avatar
JonathanTheBlack
Dragon
Posts: 544
Joined: Mon Jun 28, 2010 12:12 pm
Location: I'm the worm...

Re: Anyone here know how to use jQuery?

Post by JonathanTheBlack »

Doesn't seem to be working, but at least it's got me going in the right direction. :) Thank you.

Code: Select all

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
   function load() {
      var c = $("#content");
      c.html( c.html().replace(/\[([^\]]*)\]/g,"<a href="$1.html">$1</a>") );
   }
</script>
</head>
<body onload="load()">
	<div id="content">
		This is a [test].
	</div>
</body>
</html>

User avatar
syntruth
Giant
Posts: 241
Joined: Mon Aug 18, 2008 7:15 pm
Location: Michigan, USA

Re: Anyone here know how to use jQuery?

Post by syntruth »

Don't use onload calls; there are issues with doing so. jQuery provides a "ready" action for this:

Code: Select all

$('document').ready(function () {

  /* Loop through each .content div and replace as needed. */
  $(".content").each(function() {
    var c    = $(this),
        html = c.html().replace(/\[([^\]]*)\]/g, "<a href='$1.html'>$1</a>");
    c.html(html);
  });
});

...also, better to use a class on the divs you want to have replacement happen in, since a page can have at most one element with a given #id, and jQuery honors this. If you use $() to search for an #id element and there are more than one on the page, jQuery will always return the first one found. I've changed to using '.content' in the code above.

EDIT: Also, might I suggest http://stackoverflow.com/ for questions like this; very helpful folks there. :)

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Anyone here know how to use jQuery?

Post by aliasmask »

JonathanTheBlack wrote:Doesn't seem to be working, but at least it's got me going in the right direction. :) Thank you.

Code: Select all

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
   function load() {
      var c = $("#content");
      c.html( c.html().replace(/\[([^\]]*)\]/g,'<a href="$1.html">$1</a>') );
   }
</script>
</head>
<body onload="load()">
	<div id="content">
		This is a [test].
	</div>
</body>
</html>
I changed the above to have the correct nesting of quotes.

edit: Yeah, I would suggest using ready as well. If onload is used by any other scripts, only the last one will take effect. ready actually queues up the onload scripts. It's been awhile, I forgot about using ready. And using a class is better for multiple locations.

Code: Select all

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$('document').ready(function () {

  /* Loop through each .content div and replace as needed. */
  $(".content").each(function() {
    var c    = $(this),
        html = c.html().replace(/\[([^\]]*)\]/g, '<a href="$1.html">$1</a>');
    c.html(html);
  });
});
</script>
</head>
<body>
   <div class="content">
      This is a [test] inside a div.
   </div>
   <table><tr><td>Test [column] 1 with no class</td><td class="content">Test [column] 2 inside a table</td></tr></table>
</body>
</html>

User avatar
JonathanTheBlack
Dragon
Posts: 544
Joined: Mon Jun 28, 2010 12:12 pm
Location: I'm the worm...

Re: Anyone here know how to use jQuery?

Post by JonathanTheBlack »

Awesome. Got the test page to work, but when I tried to make it a function and move it into a .js file, it stopped working. :(

Never mind. Figured out what I did wrong by trial and error!

Yay it's working. I messed around with RegExr and now it will do bold and italic text as well. I think the last thing I really need is how to do bold and italic at the same time without screwing up something else.

http://dl.dropbox.com/u/33654550/index.html
http://dl.dropbox.com/u/33654550/scripts/simple-wiki.js

I can finally start adding content. Yay.

User avatar
jfrazierjr
Deity
Posts: 5176
Joined: Tue Sep 11, 2007 7:31 pm

Re: Anyone here know how to use jQuery?

Post by jfrazierjr »

JonathanTheBlack wrote:Awesome. Got the test page to work, but when I tried to make it a function and move it into a .js file, it stopped working. :(

Never mind. Figured out what I did wrong by trial and error!

Yay it's working. I messed around with RegExr and now it will do bold and italic text as well. I think the last thing I really need is how to do bold and italic at the same time without screwing up something else.

http://dl.dropbox.com/u/33654550/index.html
http://dl.dropbox.com/u/33654550/scripts/simple-wiki.js

I can finally start adding content. Yay.
Put a class onto the link element and define the class CSS properly.. done...

Code: Select all

c.html( c.html().replace(/\[([^\]]*)\]/g,'<a href="$1.html" class="myclass" >$1</a>') );
I save all my Campaign Files to DropBox. Not only can I access a campaign file from pretty much any OS that will run Maptool(Win,OSX, linux), but each file is versioned, so if something goes crazy wild, I can always roll back to a previous version of the same file.

Get your Dropbox 2GB via my referral link, and as a bonus, I get an extra 250 MB of space. Even if you don't don't use my link, I still enthusiastically recommend Dropbox..

User avatar
JonathanTheBlack
Dragon
Posts: 544
Joined: Mon Jun 28, 2010 12:12 pm
Location: I'm the worm...

Re: Anyone here know how to use jQuery?

Post by JonathanTheBlack »

Ah, that's not what I meant. :)

I mean **taking** //this// and **//that//** and turning it into:

taking this and that without some screwy problems with nested syntax.


It's much easier when writing and reading html to just be able to [link] **bold** and //italic// text inline this way than it is to <A href="link.html">link</a> <span style="font-weight: bold;">bold</span> and <span style="font style: italic;">italic</span> with all that html soup.

User avatar
JonathanTheBlack
Dragon
Posts: 544
Joined: Mon Jun 28, 2010 12:12 pm
Location: I'm the worm...

Re: Anyone here know how to use jQuery?

Post by JonathanTheBlack »

I've decided to not use this stuff for formatting and am instead using it just for links and glossary links. Is there an easy way to remove the white spaces from $1 or replace them with a single dash?

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Anyone here know how to use jQuery?

Post by Azhrei »

JonathanTheBlack wrote:Is there an easy way to remove the white spaces from $1 or replace them with a single dash?
Hm. If you want something that is portable across most (all?) browsers, the quickest way is probably to split the string up wherever there's a space, then join the resulting array back together again. The .split() and .join() methods of the String object are quite reliable between browser versions.

Post Reply

Return to “General Discussion”