jsf - Execute JavaScript from a Composite Component - Stack Overflow

Good day, I have a problem that has annoyed me for a couple of hours now. It's very straight forwa

Good day, I have a problem that has annoyed me for a couple of hours now. It's very straight forward. I try to invoke/execute a JavaScript when the posite ponent is rendered. Like what you can do with the html tag body and onload.

How can I reference the in-line JavaScript to be executed ?

<cc:implementation>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="" />
    <div id="#{cc.clientId}" >

        <div id="map_canvas" style="width: 850px; height: 350px; padding: 1px; border: darkblue" />

        <script>init();</script>

        <script type="text/javascript">
            var map = null;
            function init() {
                var latlng = new google.maps.LatLng(51.5001524, -0.12623619);
                var myOptions = {
                    zoom: 7,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            }
        </script>
    </div>
    </cc:implementation>

I have tried with this.init() , #{cc.clientId}.init() .But the JS can't be found in the context. If I execute it from a JSF/ajax ponent it works fine with just "init();"

Good day, I have a problem that has annoyed me for a couple of hours now. It's very straight forward. I try to invoke/execute a JavaScript when the posite ponent is rendered. Like what you can do with the html tag body and onload.

How can I reference the in-line JavaScript to be executed ?

<cc:implementation>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://maps.google./maps/api/js?sensor=false" />
    <div id="#{cc.clientId}" >

        <div id="map_canvas" style="width: 850px; height: 350px; padding: 1px; border: darkblue" />

        <script>init();</script>

        <script type="text/javascript">
            var map = null;
            function init() {
                var latlng = new google.maps.LatLng(51.5001524, -0.12623619);
                var myOptions = {
                    zoom: 7,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            }
        </script>
    </div>
    </cc:implementation>

I have tried with this.init() , #{cc.clientId}.init() .But the JS can't be found in the context. If I execute it from a JSF/ajax ponent it works fine with just "init();"

Share Improve this question edited Jun 15, 2011 at 14:24 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Jun 15, 2011 at 14:09 ChrisChris 7124 gold badges16 silver badges40 bronze badges 2
  • It works fine also when i put the JS in seperate file and include it with <h:outputScript> tag. – Chris Commented Jun 15, 2011 at 14:11
  • Why do you have the call to "init()" inside a separate <script> from the definition of the function? It should be in the same <script>, and after the function definition. – Pointy Commented Jun 15, 2011 at 14:15
Add a ment  | 

1 Answer 1

Reset to default 5

You're calling init() before the function has been definied.

Put the call after the function definition.

<script type="text/javascript">
    var map = null;
    function init() {
        var latlng = new google.maps.LatLng(51.5001524, -0.12623619);
        var myOptions = {
            zoom: 7,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }
</script>
<script>init();</script>

Or just get rid of the function call and just execute it directly. It'll just work as it's been executed after <div id="map_canvas"> has been definied.

<script type="text/javascript">
    var map = null;
    var latlng = new google.maps.LatLng(51.5001524, -0.12623619);
    var myOptions = {
        zoom: 7,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
</script>

Unrelated to the concrete problem: you've another design flaw in your posite ponent. This ponent cannot be reused more than once in the same view. You would end up with multiple <div id="map_canvas"> elements in the HTML. Having multiple elements with the same id in HTML is illegal. Fix it accordingly:

<div id="#{cc.clientId}_map_canvas" ...>

and

... document.getElementById("#{cc.clientId}_map_canvas") ...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205730a4616584.html

相关推荐

  • jsf - Execute JavaScript from a Composite Component - Stack Overflow

    Good day, I have a problem that has annoyed me for a couple of hours now. It's very straight forwa

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信