You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Alex Kotchnev <ak...@gmail.com> on 2012/07/17 16:14:03 UTC

Conditionally (IE) including javascript

I would like to conditionally include a javascript file if a specific
browser is using my app:

<!--[if lt IE 9]>
          <script
src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
 <![endif]-->



The CSS page ( http://tapestry.apache.org/css.html )  provides an example
of how to generate a conditional inclusion of a stylesheet for IE. How can
I accomplish the same for a javascript file ? There doesn't seem to be an
equivalent "JavascriptLink" with "JavascriptOptions" classes available).

@Environmentalprivate JavaScriptSupport javaScriptSupport;

@Inject @Path("context:layout/ie-only.css")private Asset ieOnlyStylesheet;
// add an IE-only style sheet if browser is IEvoid afterRender() {
    javaScriptSupport.importStylesheet(new StylesheetLink(ieOnlyStylesheet, new
                        StylesheetOptions(null, "IE")) );
}

To accomplish:

<!--[if IE]><link type="text/css" rel="stylesheet"
href="/assets/1.0-SNAPSHOT/ctx/layout/ie-only.css"></link><![endif]-->


Cheers,

Alex K

Re: Conditionally (IE) including javascript

Posted by George Christman <gc...@cardaddy.com>.
My bad, I should of read your question a bit more thoroughly. This is by far
the an elegant solution, but would accomplish your goals. Use
request.getParameter("User-Agent") to get your browser type and surround
javaScriptSupport with a conditional statement. 

  @Inject @Path("${tapestry.scriptaculous}/dragdrop.js")
  private Asset dragDropLibrary;

  @Environmental
  private JavaScriptSupport javaScriptSupport;

  @Inject 
  private Request request;

  void setupRender() {    
     if(browserCondition) {
         javaScriptSupport.addScriptLink(dragDropLibrary);
     }
  }
 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Conditionally-IE-including-javascript-tp5714506p5714578.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Conditionally (IE) including javascript

Posted by Alex Kotchnev <ak...@gmail.com>.
George,
   yep, I had seen this example on conditionally including stylesheets.
However, I was wondering how to do the same w/ javascript...

Cheers,

Alex K

On Wed, Jul 18, 2012 at 12:23 PM, George Christman
<gc...@cardaddy.com>wrote:

> You could do something like this,
>
>     @Inject
>     @Path("context:css/ie-only.css")
>     private Asset ieOnlyStylesheet;
>
>     // add an IE-only style sheet if browser is IE 7 || IE 8
>     void afterRender() {
>         this.js.importStylesheet(
>             new StylesheetLink(ieOnlyStylesheet,
>                 new StylesheetOptions(null).withCondition("(IE 7)|(IE 8)")
>             )
>         );
>     }
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Conditionally-IE-including-javascript-tp5714506p5714568.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Conditionally (IE) including javascript

Posted by George Christman <gc...@cardaddy.com>.
You could do something like this, 

    @Inject 
    @Path("context:css/ie-only.css")
    private Asset ieOnlyStylesheet;

    // add an IE-only style sheet if browser is IE 7 || IE 8
    void afterRender() {
        this.js.importStylesheet(
            new StylesheetLink(ieOnlyStylesheet, 
                new StylesheetOptions(null).withCondition("(IE 7)|(IE 8)")
            )
        ); 
    }

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Conditionally-IE-including-javascript-tp5714506p5714568.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Conditionally (IE) including javascript

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Mon, 13 Aug 2012 15:57:17 -0300, Alex Kotchnev <ak...@gmail.com>  
wrote:

> Steve - thanks a lot, this is a very clean and nice solution to this  
> issue. Very nice !

Very nice indeed. :) It's quite nice to see how simple the solutions  
people find when using Tapestry. By the way, I have one suggestion: return  
false in beginRender to avoid content put inside the component body to be  
rendered.

>
> Cheers,
>
> Alex K
>
> On Sun, Aug 12, 2012 at 5:34 AM, Steve Eynon  
> <steve.eynon@alienfactory.co.uk
>> wrote:
>
>> I use this little component:
>>
>> import org.apache.tapestry5.MarkupWriter;
>> import org.apache.tapestry5.annotations.Parameter;
>>
>> public class IfIe {
>>
>>     @Parameter(required=true, defaultPrefix="literal")
>>     private String test;
>>
>>     public void beginRender(MarkupWriter writer) {
>>         // write out a dummy tag to prevent T5 from writing it's
>> script tags 'inside' the IE comment!
>>         writer.element("script", "type", "text/javascript");
>>         writer.end();
>>
>>         writer.writeRaw("\n<!--[if ");
>>         writer.writeRaw(test);
>>         writer.writeRaw("]>\n");
>>     }
>>
>>     public void afterRender(MarkupWriter writer) {
>>         writer.writeRaw("\n<![endif]-->\n");
>>     }
>> }
>>
>> <html>
>> <head>
>>     <title>Feel The Pain</title>
>>     <t:ifIe test="lt IE 9">
>>         <script type="text/javascript"
>> src="${asset:context:/javascript/html5shiv-rc1.js}"></script>
>>     </t:ifIe>
>> </head>
>>   ...
>>
>> --
>> Steve Eynon
>> -------------------------------
>> "If at first you don't succeed,
>>    so much for skydiving!"
>>
>>
>>
>> On 19 July 2012 09:10, Alex Kotchnev <ak...@gmail.com> wrote:
>> > I'll give George's suggestion a go tomorrow. It seems like I should be
>> able
>> > to do this from the layout components that pages use, so that I don't
>> have
>> > to add the code to each page.
>> >
>> > Cheers,
>> >
>> > Alex K
>> > On Wed, Jul 18, 2012 at 4:51 PM, trsvax <tr...@gmail.com> wrote:
>> >
>> >> For this particular case I suspect you just want this on every page  
>> in
>> the
>> >> site.
>> >>
>> >> If so the simplest way would be just include the following in your
>> >> layout.tml file
>> >>
>> >> &lt;!--[if lt IE 9]&gt;
>> >>
>> >>  &lt;![endif]--&gt;
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://tapestry.1045711.n5.nabble.com/Conditionally-IE-including-javascript-tp5714506p5714581.html
>> >> Sent from the Tapestry - User mailing list archive at Nabble.com.
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> >> For additional commands, e-mail: users-help@tapestry.apache.org
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>


-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Conditionally (IE) including javascript

Posted by Alex Kotchnev <ak...@gmail.com>.
Steve - thanks a lot, this is a very clean and nice solution to this issue.
Very nice !

Cheers,

Alex K

On Sun, Aug 12, 2012 at 5:34 AM, Steve Eynon <steve.eynon@alienfactory.co.uk
> wrote:

> I use this little component:
>
> import org.apache.tapestry5.MarkupWriter;
> import org.apache.tapestry5.annotations.Parameter;
>
> public class IfIe {
>
>     @Parameter(required=true, defaultPrefix="literal")
>     private String test;
>
>     public void beginRender(MarkupWriter writer) {
>         // write out a dummy tag to prevent T5 from writing it's
> script tags 'inside' the IE comment!
>         writer.element("script", "type", "text/javascript");
>         writer.end();
>
>         writer.writeRaw("\n<!--[if ");
>         writer.writeRaw(test);
>         writer.writeRaw("]>\n");
>     }
>
>     public void afterRender(MarkupWriter writer) {
>         writer.writeRaw("\n<![endif]-->\n");
>     }
> }
>
> <html>
> <head>
>     <title>Feel The Pain</title>
>     <t:ifIe test="lt IE 9">
>         <script type="text/javascript"
> src="${asset:context:/javascript/html5shiv-rc1.js}"></script>
>     </t:ifIe>
> </head>
>   ...
>
> --
> Steve Eynon
> -------------------------------
> "If at first you don't succeed,
>    so much for skydiving!"
>
>
>
> On 19 July 2012 09:10, Alex Kotchnev <ak...@gmail.com> wrote:
> > I'll give George's suggestion a go tomorrow. It seems like I should be
> able
> > to do this from the layout components that pages use, so that I don't
> have
> > to add the code to each page.
> >
> > Cheers,
> >
> > Alex K
> > On Wed, Jul 18, 2012 at 4:51 PM, trsvax <tr...@gmail.com> wrote:
> >
> >> For this particular case I suspect you just want this on every page in
> the
> >> site.
> >>
> >> If so the simplest way would be just include the following in your
> >> layout.tml file
> >>
> >> &lt;!--[if lt IE 9]&gt;
> >>
> >>  &lt;![endif]--&gt;
> >>
> >> --
> >> View this message in context:
> >>
> http://tapestry.1045711.n5.nabble.com/Conditionally-IE-including-javascript-tp5714506p5714581.html
> >> Sent from the Tapestry - User mailing list archive at Nabble.com.
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> >> For additional commands, e-mail: users-help@tapestry.apache.org
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Conditionally (IE) including javascript

Posted by Steve Eynon <st...@alienfactory.co.uk>.
I use this little component:

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.Parameter;

public class IfIe {

    @Parameter(required=true, defaultPrefix="literal")
    private String test;

    public void beginRender(MarkupWriter writer) {
        // write out a dummy tag to prevent T5 from writing it's
script tags 'inside' the IE comment!
        writer.element("script", "type", "text/javascript");
        writer.end();

        writer.writeRaw("\n<!--[if ");
        writer.writeRaw(test);
        writer.writeRaw("]>\n");
    }

    public void afterRender(MarkupWriter writer) {
        writer.writeRaw("\n<![endif]-->\n");
    }
}

<html>
<head>
    <title>Feel The Pain</title>
    <t:ifIe test="lt IE 9">
        <script type="text/javascript"
src="${asset:context:/javascript/html5shiv-rc1.js}"></script>
    </t:ifIe>
</head>
  ...

--
Steve Eynon
-------------------------------
"If at first you don't succeed,
   so much for skydiving!"



On 19 July 2012 09:10, Alex Kotchnev <ak...@gmail.com> wrote:
> I'll give George's suggestion a go tomorrow. It seems like I should be able
> to do this from the layout components that pages use, so that I don't have
> to add the code to each page.
>
> Cheers,
>
> Alex K
> On Wed, Jul 18, 2012 at 4:51 PM, trsvax <tr...@gmail.com> wrote:
>
>> For this particular case I suspect you just want this on every page in the
>> site.
>>
>> If so the simplest way would be just include the following in your
>> layout.tml file
>>
>> &lt;!--[if lt IE 9]&gt;
>>
>>  &lt;![endif]--&gt;
>>
>> --
>> View this message in context:
>> http://tapestry.1045711.n5.nabble.com/Conditionally-IE-including-javascript-tp5714506p5714581.html
>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Conditionally (IE) including javascript

Posted by Alex Kotchnev <ak...@gmail.com>.
I'll give George's suggestion a go tomorrow. It seems like I should be able
to do this from the layout components that pages use, so that I don't have
to add the code to each page.

Cheers,

Alex K
On Wed, Jul 18, 2012 at 4:51 PM, trsvax <tr...@gmail.com> wrote:

> For this particular case I suspect you just want this on every page in the
> site.
>
> If so the simplest way would be just include the following in your
> layout.tml file
>
> &lt;!--[if lt IE 9]&gt;
>
>  &lt;![endif]--&gt;
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Conditionally-IE-including-javascript-tp5714506p5714581.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Conditionally (IE) including javascript

Posted by trsvax <tr...@gmail.com>.
For this particular case I suspect you just want this on every page in the
site.

If so the simplest way would be just include the following in your
layout.tml file

&lt;!--[if lt IE 9]&gt;
          
 &lt;![endif]--&gt;

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Conditionally-IE-including-javascript-tp5714506p5714581.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org