views:

71

answers:

2

Hi everybody,

I'm looking for a good way on preventing links in my rails app having any effect while a swf file is active...

Can I do this with a before_filter? Or do I need javascript?

Markus

A: 

You will need to use JavaScript for that, but if you use a library such as jQuery, it's easy peasy.

You can simply create a JS variable once your flash content is loaded, and then:

$("a#click").onclick = function() { return false; }

This will disable all the links on the page.

To re-enable, simply:

$("a#click").onclick = function() { return true; }

And all the links will be re-enabled again. obviously you will need put some logic for enabling and disabling the links, but the basis for it (my own approach) are as described above.

Hope it helps you

UPDATE

As you mentioned you don't have much experience with jQuery or JavaScript, I wrote an example that will do it even better.

Instead of disabling the links (which isn't very user friendly), why don't you simply hide them?

I put an example of it here:, but the code will do something like:

function changeLinks(mode){
  if(mode == "enable"){  
      alert("links enabled")
      $("a").toggle(); 
  }
  else{
      alert("links disabled")
      $("a").toggle(); 
  }
}

To add jQuery to your page, it's a easy as:

<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;

And there's a great introduction to jQuery with Rails here

Marcos Placona
Hi mplacona, thanks for your response, but I don't really get it yet. What is jQuery, and how can I include it? Where do I put this code? and is a#click a name variable or what? As you see Im quite a newby at javascript...
Markus
jQuery is a JavaScript library, much like Scriptaculous, Dojo etc... There's a great intro to it's integration with a rail project on this link:http://jimneath.org/2008/06/18/using-jquery-with-ruby-on-rails/
Marcos Placona
A: 

@mplacona's response is fine, but the question is HOW you will get these functions called when your SWF is deactivated. If you have access to the Flash source that created the SWF, you'll need to call the functions using ExternalInterface, but I can't quickly find how to detect when the Stage has been made invisible (it's an event handler on the Stage, for sure). However, you are no doubt doing that in ActionScript, so wherever you call the close, you can also call the ExternalInterface method which invokes the link-enabling javascript.

You would use something like this:

ExternalInterface.call('changeLinks', 'enable');

in your ActionScript.

Hope that helps somewhat.

Yar