LibWeb: Allow navigating to a new URL by setting window.location.href

This commit is contained in:
Andreas Kling 2020-05-18 21:52:50 +02:00
parent 1ec4db04cd
commit efdfdbabdb
Notes: sideshowbarker 2024-07-19 06:29:03 +09:00
5 changed files with 32 additions and 3 deletions

View file

@ -2,9 +2,13 @@
<html>
<head>
<title>window.location test</title>
<style>
#clickme { background-color: red; color: yellow; }
</style>
</head>
<body>
<pre></pre>
<pre></pre>
<div id="clickme">Click me to navigate away!</div>
<script>
var pre = document.querySelectorAll("pre")[0];
pre.innerHTML += "href: " + location.href + '\n';
@ -12,6 +16,10 @@
pre.innerHTML += "pathname: " + location.pathname+ '\n';
pre.innerHTML += "hash: " + location.hash + '\n';
pre.innerHTML += "search: " + location.search + '\n';
document.getElementById("clickme").addEventListener("mousedown", function() {
window.location.href = 'http://serenityos.org/';
});
</script>
</body>
</html>

View file

@ -54,9 +54,13 @@ JS::Value LocationObject::href_getter(JS::Interpreter& interpreter)
return JS::js_string(interpreter, window.impl().document().url().to_string());
}
void LocationObject::href_setter(JS::Interpreter&, JS::Value)
void LocationObject::href_setter(JS::Interpreter& interpreter, JS::Value value)
{
// FIXME: Navigate to a new URL
auto& window = static_cast<WindowObject&>(interpreter.global_object());
auto new_href = value.to_string(interpreter);
if (interpreter.exception())
return;
window.impl().did_set_location_href({}, new_href);
}
JS::Value LocationObject::pathname_getter(JS::Interpreter& interpreter)

View file

@ -30,7 +30,10 @@
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/MarkedValueList.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/Frame.h>
#include <LibWeb/HtmlView.h>
namespace Web {
@ -105,4 +108,15 @@ void Window::cancel_animation_frame(i32 id)
GUI::DisplayLink::unregister_callback(id);
}
void Window::did_set_location_href(Badge<Bindings::LocationObject>, const String& new_href)
{
auto* frame = document().frame();
if (!frame)
return;
auto* view = frame->html_view();
if (!view)
return;
view->load(new_href);
}
}

View file

@ -49,6 +49,8 @@ public:
void set_interval(JS::Function&, i32);
void set_timeout(JS::Function&, i32);
void did_set_location_href(Badge<Bindings::LocationObject>, const String& new_href);
private:
explicit Window(Document&);

View file

@ -67,6 +67,7 @@ class EventTargetWrapper;
class HTMLCanvasElementWrapper;
class HTMLImageElementWrapper;
class ImageDataWrapper;
class LocationObject;
class MouseEventWrapper;
class NodeWrapper;
class WindowObject;